Featured Image for Post Categories

Community Forums Forums Design Tips and Tricks Featured Image for Post Categories

This topic is: not resolved

This topic contains 6 replies, has 2 voices, and was last updated by  AC 1 year, 6 months ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #36164

    AC
    Blocked
    Post count: 7712

    I have 5 Post Categories. For each category my client wants to be able to update the featured image to showcase that month’s charts or graphs as it pertains to that category’s blog entries. First I thought to create individual pages which would allow me to use the Featured Image and use the SEO to forward that page to the Category page – but that wouldn’t work.

    So… does anyone have any idea how I can do this? Thank you much.

    #36206

    David Chu
    Participant
    Post count: 1373

    Hi,
    Yes. :-) I like your idea. Following your scheme, I wrote some code to that effect. As you can see, it checks for the category, and if it’s in that category, you grab the Featured Image from a particular page, whose number can be found by hovering over the title in the admin (235 in this example). I used a Post for testing, but a Page should work equally well. You could just repeat that code block to correspond to other Categories and other Page/Posts.

    Then the client or you could just change the Featured Image on those particular Pages to freshen things up.

    Some styling possibly needed, obviously. One could also add some more HTML to have a nice DIV for it or what-have-you. And of course, other hooks could be used besides genesis_before_loop.

    [php]
    add_action(‘genesis_before_loop’, ‘dc_put_in_featured’);
    function dc_put_in_featured() {
    if ( is_category( ‘Category #1′ ) ) {
    echo get_the_post_thumbnail(235, ‘full’);
    }
    }
    [/php]

    Cheers, Dave


    Dave Chu · Custom WordPress Developer – likes collaborating with Designers

    #36207

    AC
    Blocked
    Post count: 7712

    Thank you David. So if I have 5 categories – just add the action 5 separate times, i.e., Category 1, Category 2, etc., etc., with their specific page ids?

    #36214

    David Chu
    Participant
    Post count: 1373

    YW!
    You could do that. But it’s cleaner to just repeat this inner block inside the function, and change the code for each Category and Post. Here’s a quick example:

    [php]
    add_action(‘genesis_before_loop’, ‘dc_put_in_featured’);
    function dc_put_in_featured() {
    if ( is_category( ‘Category #1′ ) ) {
    echo get_the_post_thumbnail(235, ‘full’);
    }
    if ( is_category( ‘Category #2′ ) ) {
    echo get_the_post_thumbnail(123, ‘full’);
    }
    }
    [/php]


    Dave Chu · Custom WordPress Developer – likes collaborating with Designers

    #36218

    AC
    Blocked
    Post count: 7712

    Beautiful! It works B-E-A-U-T-F-U-L!!! Thank you!

    #36221

    David Chu
    Participant
    Post count: 1373

    Nice!!
    One could also do this via Custom Fields, too. There are frequently many ways to do a particular task.

    Dave


    Dave Chu · Custom WordPress Developer – likes collaborating with Designers

    #36222

    AC
    Blocked
    Post count: 7712

    I think for my client – the way you coded it will be the easiest way to explain it to him. Very straight forward.

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Featured Image for Post Categories’ is closed to new replies.