Skip to:
Content
Pages
Categories
Search
Top
Bottom

Use Featured Images as Category Header image

  • Hi all,
    I’ve been looking for a way to make the category archive pages use the featured image of the latest post in that category. After several days of dead ends, I decided to look at the code and found a very easy and simple solution that works like a charm. To save others the frustration, here’s what I did – (make sure you save a backup copy of the functions.php file before starting!)

    I’m using the default BuddyPress theme
    1. Find: (site root)/wp-content/plugins/buddypress/bp-themes/bp-default/functions.php
    2. In the functions.php file, near line 329, look for: `function bp_dtheme_header_style() {`
    3. You’ll see a line that starts with: `if ( is_singular() &&`… and ends with `} else {`
    4. What I did was copy that block of code and paste it in right after it, changing the condition from `is_singular()` to `is_archive()` and changing the first `} else {` to `} else if ( is_archive()…`

    The new code looks like this:
    `
    function bp_dtheme_header_style() {
    global $post;

    $header_image = ”;

    if ( is_singular() && current_theme_supports( ‘post-thumbnails’ ) && has_post_thumbnail( $post->ID ) ) {
    $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), ‘post-thumbnail’ );

    // $src, $width, $height
    if ( !empty( $image ) && $image[1] >= HEADER_IMAGE_WIDTH )
    $header_image = $image[0];
    } else if ( is_archive() && current_theme_supports( ‘post-thumbnails’ ) && has_post_thumbnail( $post->ID ) ) {
    $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), ‘post-thumbnail’ );

    // $src, $width, $height
    if ( !empty( $image ) && $image[1] >= HEADER_IMAGE_WIDTH )
    $header_image = $image[0];
    } else {
    $header_image = get_header_image();
    }
    `
    This works great for me. Hope it works for you!
    Maybe this could be added to a future release as an option in the dashboard…

  • The topic ‘Use Featured Images as Category Header image’ is closed to new replies.
Skip to toolbar