- This topic is empty.
-
AuthorPosts
-
January 10, 2015 at 4:02 am #192808taxicssParticipant
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.January 10, 2015 at 8:39 am #192819drose379ParticipantHave 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"] <= 5) {
echo $thisArray{"keyYouWantToDisplay"};
}January 10, 2015 at 8:45 am #192820taxicssParticipant@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.January 10, 2015 at 10:16 am #192823AnonymousInactiveThe 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 bywp_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
January 10, 2015 at 1:56 pm #192833SenffParticipantInstead 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(); ?>
January 10, 2015 at 11:39 pm #192854January 11, 2016 at 10:46 pm #236695Tiaan79Participant@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…January 13, 2016 at 10:47 am #236754SenffParticipant@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.
January 14, 2016 at 12:40 am #236775 -
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.