WordPress.org

Forums

[resolved] How to to call featured image of most recent post (4 posts)

  1. devi9-1
    Member
    Posted 1 year ago #

    This has been a bit of a head-scratcher for me. I am trying to customize a theme and I want to make the featured image of the most recent blog post of a specific category the main image of the blog page. So I would load this image before the nav menu on a custom blog page.

    Does anyone have any ideas on how one would do this?

    Thanks!

  2. alchymyth
    Forum Moderator
    Posted 1 year ago #

    something like:

    <?php
    $latest = get_posts( array( 'posts_per_page' => 1, 'category_name' => 'specific', 'meta_key' => '_thumbnail_id', 'orderby' => 'date', 'order' => 'DESC' );
    if( $latest ) echo get_the_post_thumbnail( $latest[0]->ID );
    ?>

    http://codex.wordpress.org/Template_Tags/get_posts
    http://codex.wordpress.org/Class_Reference/WP_Query
    https://codex.wordpress.org/Function_Reference/get_the_post_thumbnail

  3. devi9-1
    Member
    Posted 1 year ago #

    Thanks! I will try it out

  4. devi9-1
    Member
    Posted 1 year ago #

    It worked. Thank you for that!

Topic Closed

This topic has been closed to new replies.

About this Topic