I don’t know PHP all that well so I hope I will be showing enough code when I ask this question. I have a part of my homepage that is going to show the latest 5 blog posts and so I set it up like this:
<?php
function get_latest_post_html() {
$content = "";
query_posts('showposts=5');
while (have_posts()){
the_post();
$content .= "<p class='title'><a href='" . get_permalink() . "'>" . get_the_title() . "</a></p>\n" .
"<p class='excerpt'><a href='" . get_permalink() . "'><img src='" . wp_get_attachment_url( get_post_thumbnail_id($post->ID) ) . "' class='rt-image img-left wp-post-image' style='max-width:175px;'/></a>" . get_the_excerpt() . "</p><br/><hr/>";
}
wp_reset_query();
return "<div class='latest-post'>\n$content\n</div>";
}
add_shortcode('get_latest_post', 'get_latest_post_html');
?>
It calls the last 5 posts just fine, but I don’t want to have it display the <hr/> on the bottom of the 5th post.
Setup some logic in your
whileloop to conditionally display the<hr >.For example:
Note: WordPress may not return 5 posts, so you should consider that path. I would also discourage string concatenation in tight loops. Refactor your code and use
echo.