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

  • raindance



    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


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

    Anyone have any ideas?


    Thanks in advance.



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



    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:



    * The category archive template file


    * @package WordPress

    * @subpackage Graphene

    * @since Graphene 1.1.5




    <h1 class=”page-title”>


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



    <?php /* The category description */

    $cat_desc = category_description();

    if ($cat_desc) : ?>

    <div class=”cat-desc”>

    <?php echo $cat_desc; ?>


    <?php endif; ?>


    /* 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(); ?>



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

    The code itself is here and really well commented.


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


    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.
Do NOT follow this link or you will be banned from the site!