Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End How to display post thumbnail?

  • This topic is empty.
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #192808
    taxicss
    Participant

    I’m trying to display my latest 5 post to my homepage. I’m using this method to display the recent posts.

    <ul>
    <?php
        $args = array( 'numberposts' => '5' );
        $recent_posts = wp_get_recent_posts( $args );
        foreach( $recent_posts as $recent ){
            echo '<li><a>' .   $recent["post_title"].'</a> </li> ';
        }
    ?>
    </ul>
    

    How can I display the thumbnail?
    Thanks.

    #192819
    drose379
    Participant

    Have you thought about using a “while” loop? I’ve never worked with WordPress but I was thinking you could do something like

    $thisArray = yourArray;
    while ($thisArray["key"] &lt;= 5) {
    echo $thisArray{"keyYouWantToDisplay"};
    }

    #192820
    taxicss
    Participant

    @drose379, my code actually works because its from Codex. My problem though is I can’t figure out how to display the post thumbnails(featured image) of each post.

    I tried the_post_thumbnail(); but it doesn’t work.

    #192823
    Anonymous
    Inactive

    The documentation is a little convoluted and I don’t code with WordPress, but as far as I can gather the_post_thumbnail() won’t work because it needs to be done within the Loop (where there’s an obvious context as to which post you refer). The array returned by wp_get_recent_posts() isn’t documented, although my impression from the WP_Post documentation[1] is that it won’t have the information.

    What you need instead (I think) is get_the_post_thumbnail(). Bear in mind that your theme must support thumbnails for this to work:

    To enable Post Thumbnails, the current theme must include add_theme_support( ‘post-thumbnails’ ); in its functions.php file.

    http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail

    [1] http://codex.wordpress.org/Class_Reference/WP_Post

    #192833
    Senff
    Participant

    Instead of using the wp_get_recent_posts() function, create your own query, that you can customize a lot more:

    <?php 
    
        $args = array('showposts' => 5);
    
        $the_query = new WP_Query( $args );
    
        if( $the_query->have_posts() ): 
    
            echo '<ul>';
    
            while ( $the_query->have_posts()) : $the_query->the_post(); 
                echo '<li><a href="'.get_the_permalink().'">'.get_the_post_thumbnail().' '.get_the_title().'</a></li>';
            endwhile; 
    
            echo '</ul>';
    
        endif; 
    
        wp_reset_query(); 
    ?>
    
    #192854
    taxicss
    Participant

    @Senff Cool, thanks. Your code works perfectly. Thanks everyone.

    #236695
    Tiaan79
    Participant

    @Senff
    Thanks, it works for me but I am strugling to get it to display in tables (grids) like in this example: http://www.carnets-de-traverse.com/blog/ (4 recent posts – 2 next to each other and then 2 below them)… do you maybe have any idea how to do that? I am quite new to this and have been struggling for a few days now…

    #236754
    Senff
    Participant

    @Tiaan79 Instead of using ULs and LIs, use different markup.

    In my code above, the UL and /UL are wrapped around all the posts, and the LI and /LI around every individual post. You can easily replace that with different markup, for example:

    <?php 
    
        $args = array('showposts' => 5);
    
        $the_query = new WP_Query( $args );
    
        if( $the_query->have_posts() ): 
    
            echo '<table>';
    
            while ( $the_query->have_posts()) : $the_query->the_post(); 
                echo '<tr><td><a href="'.get_the_permalink().'">'.get_the_post_thumbnail().' '.get_the_title().'</tr></td>';
            endwhile; 
    
            echo '</table>';
    
        endif; 
    
        wp_reset_query(); 
    ?>
    

    This will put each post entry on its own line. If you want to have 2 or more posts per line/row, you’re going to have to use counters of some sort.

    #236775
    Tiaan79
    Participant

    Thanks very much @Senff!

Viewing 9 posts - 1 through 9 (of 9 total)
  • The forum ‘Back End’ is closed to new topics and replies.