an
groups-icon

Displaying Category Image as Default Fallback Featured Image

Wait a minute... This content is very very old. We no longer sell or support this theme version. New Topics
  • Version 9
    Richard Stewart
    0 points
    Beginner

    Is it possible to have a default image created for each listing based on the category that the listing is being made in?

    I have found this article, but when I tried to make the changes nothing happened.

    Does anyone have any ideas on how to make this possible and which pages I should edit to make this work?

    Thanks

    Richard

    https://www.wpbeginner.com/wp-themes/set-fallback-featured-image-based-post-category-wordpress/

    Displaying Category Image as Default Fallback Featured Image

    Now we are going to show you how to display a category image as the default fallback featured image or post thumbnail when a post does not have its own featured image.

    Note: Please backup your theme files before making any changes.

    Inside your loop, where your theme is displaying the featured image or post thumbnail, replace it with this code:

    <?php if ( has_post_thumbnail() ) : ?>

    <div class=”entry-thumbnail”>
    “><?php the_post_thumbnail(); ?>
    </div>

    <?php else :
    $category = get_the_category();
    ?>
    <div class=”entry-thumbnail”>
    “>/wp-content/uploads/category-images/<?php echo $category[0]->category_nicename ; ?>-150×150.jpg” alt=”<?php the_title(); ?>” />
    </div>

    <?php endif; ?>

    This code looks for a post thumbnail. If it finds one, then it displays the post thumbnail. Otherwise, it looks for the category a post belongs to and then displays the category image. We have added -150×150 in the image file name because this is the post thumbnail size in our demo theme. Your theme may be using a different size for post thumbnails, so you need to use that size instead.

    Please note that your theme may already have <?php if ( has_post_thumbnail() ) : ?> line and the next few lines that display post thumbnail. You can skip those lines if your theme already got them.

    That’s all, we hope this article helped you add fallback featured image based on post category

    March 4, 2014 at 10:34 am
  • Richard Stewart
    0 points
    Beginner
    Members Only Content

    This reply is for PremiumPress customers only.

    Login Now
    March 5, 2014 at 9:26 pm
  • Malgorzata Szalata
    0 points
    Beginner
    Members Only Content

    This reply is for PremiumPress customers only.

    Login Now
    March 6, 2014 at 1:09 am
  • Richard Stewart
    0 points
    Beginner
    Members Only Content

    This reply is for PremiumPress customers only.

    Login Now
    March 7, 2014 at 4:14 pm
  • Richard Stewart
    0 points
    Beginner
    Members Only Content

    This reply is for PremiumPress customers only.

    Login Now
    March 7, 2014 at 4:17 pm
  • Richard Stewart
    0 points
    Beginner
    Members Only Content

    This reply is for PremiumPress customers only.

    Login Now
    March 10, 2014 at 10:24 am

579

Views

5

Replies