Random header for category archive page from specific folder of images?

  • raindance

    #2263

    Hi-

    Syahir got me really close to getting this dialed in but I haven’t had any response for some time for the last step.

    Can anyone help me tweak this to show random headers from a specific category…it’s working great for blog posts assigned to a category by slug title, but, it’s not working for the category archive page (as in if the user just goes to the category “blog” main page where you see all the posts)

    I’m trying to create a “different” look for the “blog” part of my site.

    This is what I have so far:

    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' );

    The original thread is here

    https://forum.graphene-theme.com/graphene-support/rotating-headers-for-one-category/page/3

    But I’m stuck to get the header folder I assign to show up for the category page.

    Anyone have any ideas?

    Syahir?

    Thanks in advance.

    Josh

    #19569

    Sorry, my friend. This is beyond me. I hope to get there one day though…

    raindance

    #19570

    I just found this in my child theme folder…don’t know what it’s there for or if relevant in the evolution, but…could it be making the code you gave me Syahir not work for what I’m trying to do?? I think it’s to make the word “category archive” not show up so probably not relevant but…

    I’m still waiting Syahir for you to help me finish this off please?

    category.php in child theme folder:

    <?php

    /**

    * The category archive template file

    *

    * @package WordPress

    * @subpackage Graphene

    * @since Graphene 1.1.5

    */

    get_header();

    ?>

    <h1 class=”page-title”>

    <?php

    printf(__(‘<span>%s</span>’, ‘graphene’), single_cat_title(”, false));

    ?>

    </h1>

    <?php /* The category description */

    $cat_desc = category_description();

    if ($cat_desc) : ?>

    <div class=”cat-desc”>

    <?php echo $cat_desc; ?>

    </div>

    <?php endif; ?>

    <?php

    /* Run the loop for the category page to output the posts.

    * If you want to overload this in a child theme then include a file

    * called loop-category.php and that will be used instead.

    */

    get_template_part(‘loop’, ‘category’);

    ?>

    <?php get_footer(); ?>

    raindance

    #19571

    A really wonderful coder at the wordpress forum dialed this in beautifully for me.

    The code itself is here and really well commented.

    http://pastebin.com/7txapyW9

    The WP Forum thread is here if anyone wants to see more details about how we got there.

    http://wordpress.org/support/topic/specific-header-image-for-specific-category?replies=46

    This is for category archive pages and single posts to rotate custom header images from folders you create. It works great and is super easy to use. It doesn’t work for single pages, but I figure I can just use a single custom header on a page so it doesn’t matter too much. He may try to re-work it to include pages but maybe not. He spent a lot of time dialing this in. It’s really well done and a pleasure to have! If he does re-work it to include pages he will update the pastebin. Just subscribe to that thread for updates.

    Thanks for getting it started Syahir.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.