Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I wish to set a featured/header image for each of my category archive pages, how would I go about doing this without hard coding each category into the theme?

An example of the page is at http://keswickscouts.org/section/cubs

The code form header.php is

            <div id="main-image">
        <?php
            // Check to see if the header image has been removed
            $header_image = get_header_image();
            if ( ! empty( $header_image ) ) :
        ?>
            <a href="<?php echo esc_url( home_url( '/' ) ); ?>">
                <?php
                    if ( !is_404() && !is_attachment() ) { // Check if this is not 404 or attachment page
                        // Check if this is a post or page, if it has a post-thumbnail
                        if ( is_singular() && has_post_thumbnail( $post->ID ) && ( $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'post-thumbnail' ) ) && $image[1] >= HEADER_IMAGE_WIDTH ) :
                        echo get_the_post_thumbnail( $post->ID, 'post-thumbnail' );
                    else : ?>
                        <img src="<?php header_image(); ?>" height="<?php echo get_custom_header()->height; ?>" width="<?php echo get_custom_header()->width; ?>" alt="" />
                <?php endif; } // end check for featured image or standard header ?>
            </a>
        <?php endif; // end check for removed header image ?>
        </div><!-- end #main-image -->
share|improve this question
1  
What is the question? –  s_ha_dum Nov 6 '12 at 14:55
    
I'm seeing an issue where the "post-thumbnail" image size is probably not going to be as wide as the HEADER_IMAGE_WIDTH (I'd change it to 'full') but otherwise I'm not sure what you're asking. –  guiniveretoo Nov 6 '12 at 15:34
    
You also might want to check the code for your 404 page - it says "use the search box above" but there's no search box! –  guiniveretoo Nov 6 '12 at 15:36
    
I've reworded the question, hopefully it should make more sense. I've had no issues with the width but will have a look, and thanks for the heads up on the 404 page it is completely knackered –  The-Keswickian Nov 6 '12 at 18:43

1 Answer 1

up vote 1 down vote accepted

What I did was; using the Taxonomy Images plugin to associate an image with each category, then to my theme's header.php, below the <a> line in the above code;

<?php 
            $image_id = apply_filters( 'taxonomy-images-queried-term-image-id', 0 );
            if ( is_category() && ! empty( $image_id )):
             print apply_filters( 'taxonomy-images-queried-term-image', '',  
                array(
                'image_size' => 'header'
            ) );
            else : ?>

And add <?php endif; ?> below the endif 'end check for featured...'

and defined the header image size in functions.php with

add_image_size( 'header', 960, 260, true );
share|improve this answer
    
How can I modify this to work on single.php? Just removing the if ( is_category() ) doesn't work. –  Rev Jul 23 '13 at 18:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.