• Resolved s2pidkaspr

    (@s2pidkaspr)


    I’m using this code to display featured image on my posts.

    <?php if (has_post_thumbnail( $post->ID ) ): ?>
    <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
    <div id="custom_bg">
    <img src="<?php echo $image[0]; ?>" />
    </div>
    <?php endif; ?>

    But the featured images are shown on all posts, which is great. But i’m wondering if i can only display this code on posts based on their category?

    Any ideas?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Try this (assuming the id for the category is 20):

    <?php if ( in_category(20) ) { ?>
    <p>DO SOMETHING</p>
    <?php } else { ?>
    <p>DO SOMETHING ELSE</p>
    <?php } ?>

    with your code it would look like this:

    <?php if ( in_category(20) ) { ?>
    //do something
    <?php if (has_post_thumbnail( $post->ID ) ): ?>
    <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
    <div id="custom_bg">
    <img src="<?php echo $image[0]; ?>" />
    </div>
    <?php endif; ?>
    <?php } else { ?>
    //Do something else
    <?php } ?>

    Thread Starter s2pidkaspr

    (@s2pidkaspr)

    Works like a charm! ??
    This is what I’m actually looking for.

    Thanks.

    Glad to be of help ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to set featured image on single posts based on category?’ is closed to new replies.