Rotating headers for ONE category?

  • raindance

    #19044

    Yup these:

    But I still see only the original headers on the new blog test post which is assigned to the “blog” category…

    http://juicytravels.com/category/juicy-travels-blog/

    http://juicytravels.com/blog-post-test/

    (from above)

    Admin

    Syahir Hakim

    #19045

    Okay, firstly, I was under the impression that you want the header to rotate in the archive page, i.e. the page that lists all posts that belong to the category “Blog”.

    Secondly, the slug for your “Blog” category appears to be juicy-travels-blog, which is why the code didn’t work. It was assuming that the slug for your “Blog” category is blog.

    Thirdly, of course it wouldn’t work for individual posts that are assigned to the “Blog” category. That’s not what it’s meant for.

    However, it’s pretty easy to make the changes, but you’ve got to first make sure that the part of the code where it gets the random image work first. Try using this code:

    function graphene_custom_header_image( $image ){
    // if ( is_category( 'blog' ) ){
    /* The script to rotate through possible images here. */

    // The directory where the images reside
    $dir = get_stylesheet_directory() . '/images/custom-headers/';

    // Get all images from the $dir above
    $images = glob( $dir . "*.jpg" );

    if ( $images !== false ){
    // Pick a random image
    $key = array_rand( $images );

    // Make sure the resulting image is assigned to the $image variable
    $image = get_stylesheet_directory_uri() . '/images/custom-headers/' . basename( $images[$key] );
    }
    // }

    return $image;
    }
    add_filter( 'graphene_header_image', 'graphene_custom_header_image' );

    If that works, it will rotate the header images for *all* pages and posts in your site.

    raindance

    #19046

    emmmm…sorry if I didn’t explain it well.

    The point has always been…to have rotating/random headers from a directory for a particular category.

    Sorry if that was not clear?

    raindance

    #19047

    Am I possibly missing something basic and obvious about how categories work?

    I mean if I assign any post to a category “abc”, then I want to create a directory of random/rotating headers relevant to that category “abc” that’s what I’m after.

    Like what if a post/page category is “sky diving” and all posts or pages are assigned to that category “sky diving”…then I want to assign the header images to randomly rotate from a directory of photos called “images/custom-headers/skydiving”. I’m just saying something that seems like it should be simple for anyone to do. Instead of ONE limited featured image for a post or page, I want to assign posts/page to a category, like blog or sky diving, and have more than one photo be the header image.

    Am I not explaining it well? ๐Ÿ™‚ Sorry if so.

    Thanks Syahir.

    raindance

    #19048

    Wait I just read the last line of your post again….

    “If that works, it will rotate the header images for *all* pages and posts in your site.

    No. I don’t want it do do all pages and posts. From the beginning of this thread my point has been…I want to be able to rotate random images from a directory of images assigned per category.

    Please let me know if this is not clear.

    Admin

    Syahir Hakim

    #19049

    Like I said, it can be done and the code needs to be modified a bit to accommodate your use case, but first, we need to make sure that the part where the code randomly picks the image out works first.

    Read my post again, particularly this part:

    Quote:
    However, it’s pretty easy to make the changes, but you’ve got to first make sure that the part of the code where it gets the random image work first.

    The “If that works, it will rotate the header images for *all* pages and posts in your site.” part is a sure-fire way to make sure that that part of the code works. Then we’ll get to getting it to work according to categories.

    Also, next time, try editing your forum post to add more info rather than posting multiple posts one after the other. That’ll make it easier to follow the discussion without too much text to read through.

    raindance

    #19050

    Ah got, it. Thank you. And ok I will edit rather than add but I thought maybe it better to have the history there of the conversation for someone else trying to figure it out later.

    Sorry if I misunderstood you and if I wasn’t explaining it well. I think it has to do with my own confusion about blog posts vs categories vs pages etc.

    So…yes, you are absolutely correct! I do want it for the category archive page, I misunderstood you. I also want it for any posts assigned to the category blog. But I’d also like to make different directories show up for different categories, both archive page and individual posts, does that make sense?

    So what you gave me above works PERFECTLY for what I was trying to do…THANK YOU!

    Again, my apologies for being unclear…you knew exactly what I meant…I just confused myself ๐Ÿ™‚

    So now the only question remaining is, for example, can I have another layer of granularity and have different categories pulling from different directories, like:

    images/custom-headers/skydiving

    images/custom-headers/scubadiving

    or whatever…for relative categories?

    Would I just repeat the function above with different category name + path or can I add the variety into the same function?

    Thank you again for your patience with me and your help…I really appreciate it!!

    raindance

    #19051

    I was mistaken. After I cleared my cache, I see that now the entire site is displaying header images from that new images/custom-headers folder. In other words, it’s not differentiating category of blog vs the rest of the site.

    Yes, I see where you said what we are trying to nail down is randomly choosing…that part IS working perfectly, it is randomly displaying from the correct directory. SO now…just need to get it only pulling from that directory for the chosen category.

    As a follow up question:

    I want to make sub-categories of the category called “Blog”.

    I want to assign directories of images to those sub-categories.

    THANK YOU!

    Admin

    Syahir Hakim

    #19052

    OK, try this:

    function graphene_custom_header_image( $image ){
    if ( is_category() || is_single() ){
    /* The script to rotate through possible images here. */

    // Get the current category
    if ( $cat = get_query_var( 'cat' ) ){
    $cat = get_the_terms( $cat, 'category' );
    $cat = $cat[1]->slug;
    } else {
    global $post;
    $cat = get_the_category( $post->ID );
    $cat = $cat[0]->slug;
    }

    // The directory where the images reside
    $dir = get_stylesheet_directory() . '/images/custom-headers/' . $cat;

    if ( is_dir( $dir ) ){
    // Get all images from the $dir above
    $images = glob( $dir . "/*.jpg" );

    if ( $images ){
    // Pick a random image
    $key = array_rand( $images );

    // Make sure the resulting image is assigned to the $image variable
    $image = get_stylesheet_directory_uri() . "/images/custom-headers/$cat/" . basename( $images[$key] );
    }
    }
    }

    return $image;
    }
    add_filter( 'graphene_header_image', 'graphene_custom_header_image' );

    That will get random images for each category according to their slug. E.g. for the Uncategorized category, it will get the images from graphene-child/images/custom-headers/uncategorized/.

    raindance

    #19053

    Ok so getting closer…thank you so much for sticking with it!

    Category name is blog and category slug is juicy-travels-blog.

    The post called “blog post test” below is assigned to the category blog.

    http://juicytravels.com/category/juicy-travels-blog/

    NOT showing images from /images/custom-headers/juicy-travels-blog

    Rather is showing the general custom headers same as other “non blog” pages/posts

    http://juicytravels.com/blog-post-test/

    IS showing images randomly from the directory /images/custom-headers/juicy-travels-blog

    Rest of the site is showing the correct “general custom headers properly rotated as they were before. So that part is back to good.

    So the only issue still is the category archive page, as you correctly pointed out earlier, is not showing images from /images/custom-headers/juicy-travels-blog

    Perhaps I missed something in your instructions?

    Thank you!

Viewing 10 posts - 31 through 40 (of 53 total)

  • You must be logged in to reply to this topic.
Graphene Themes Forum