If you ultimately want the last post, regardless of pages, then you need to use found_posts
.
This could change depending on how you sort but I didn’t see any modifications to that in your code.
So to just modify the content of your code above you would do this:
<?php
if ( have_posts() ) {
$count = $GLOBALS['wp_query']->post_count;
$total = $GLOBALS['wp_query']->found_posts;
while ( have_posts() ) {
the_post();
echo '<a href="'. get_the_permalink(). '">'. get_the_title(). '</a>';
if ($count != $total) {
echo ', ';
}
}
}
wp_reset_postdata();
?>
Another way to do this could be to add the lines to an array and then implode it:
<?php
if ( have_posts() ) {
$myPosts = array();
while ( have_posts() ) {
the_post();
$myPosts[] = '<a href="'. get_the_permalink() .'">'. get_the_title() .'</a>';
}
echo implode(', ', $myPosts)
}
wp_reset_postdata();
?>
Notice I changed the_title
to get_the_title
and the_permalink()
to get_the_permalink()
so we could echo instead of just displaying the them. I also changed your concatenators from a ,
to .
.