php – Adding commas between post titles on an archive page?

Question

I am working on my archive.php, which is used by several different custom post types.

On the archive pages, I would like post titles to display with commas between them (but not after the final post), e.g.:

Title 1, Title 2, Title 3

This is the code I currently have, but my if statement isn’t working. I’m very new to PHP and WP, so any help is very much appreciated.

<?php
    if ( have_posts() ) {
        while ( have_posts() ) {
            the_post();
            echo '<a href="', the_permalink(), '">', the_title(), '</a>';
            if ((current_post) < (post_count-1)) {
                echo ', ';
            }
        }
    }

    wp_reset_postdata();

?>

0
casterapple 6 days 2022-11-24T20:43:35-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse