0

I have the following code in my single.php file:

<?php 
    if ( have_posts() ) : 
        while ( have_posts() ) : the_post(); 
            next_post_link('%link', 'Next', TRUE); 
            previous_post_link('%link', 'Previous', TRUE);

            the_title(); 
            the_content(); 

        endwhile; 
    endif; 
?>

What I would like to do is count the number of posts in a category and use the next_post_link and previous_post_link link to navigate through them, then display a count.

So, if the post is the second one in the category the output would look like (Post 2 of 20), if it were the third the output would look like (Post 3 of 20), etc.

Any help or direction is appreciated :-)

Thanks,
Josh

  • I would believe that a post is only assigned to one category then – Pieter Goosen Jul 16 '14 at 6:11
2

Ok,

So, this is a two part question, part #1:

What I would like to do is count the number of posts in a category and use the next_post_link and previous_post_link link to navigate through them, then display a count.

What I did to fix this issue was install the Smarter Navigation plugin

Then use the following for my Previous and Next Links:

next_post_smart("%link", "Next", TRUE);
previous_post_smart("%link", "Previous", TRUE);

The next part is a little bit more tricky...

Part #2:

So, if the post is the second one in the category the output would look like (Post 2 of 20), if it were the third the output would look like (Post 3 of 20), etc.

Now, to fix this one I used the following function:

  1. Display and get the post number (which is a tweaked version of what I found here):

    function Get_Post_Number($postID){
        $temp_query = $wp_query;
        $referrer_cat = get_referrer_category();
        $referrer_cat_id = ( is_object( $referrer_cat ) ) ? $referrer_cat->term_id : false;
        $postNumberQuery = new WP_Query('cat='.$referrer_cat_id.'&orderby=date&posts_per_page=-1');
        $counter = 1;
        $postCount = 0;
        if($postNumberQuery->have_posts()) : while ($postNumberQuery->have_posts()) : $postNumberQuery->the_post();
            if ($postID == get_the_ID()) {
                $postCount = $counter;
            } else {
                $counter++;
            }
            endwhile; endif;
        wp_reset_query();
        $wp_query = $temp_query;
        return $postCount;
    }
    

Basically the differences is...

  1. I get my two referrer variables from the Smarter Navigation plugin: referrer_cat and referrer_cat_id and add them to my function (Lines two and three of my function):

    $referrer_cat = get_referrer_category();
    $referrer_cat_id = ( is_object( $referrer_cat ) ) ? $referrer_cat->term_id : false;
    
  2. I query the current category from my referrer variable that I added to the function - look at the $postNumberQuery (fourth line town from the start of the function) at the start of new WP_Query I added 'cat='.$referrer_cat_id.'&

So, now my final code goes from:

This:

<?php 
if ( have_posts() ) : 
    while ( have_posts() ) : the_post(); 
        next_post_link('%link', 'Next', TRUE); 
        previous_post_link('%link', 'Previous', TRUE); 

        the_title(); 
        the_content(); 

    endwhile; 
endif; 
?>

To This:

<?php 
    if ( have_posts() ) : 

        while ( have_posts() ) : the_post(); 

            $referrer_cat = get_referrer_category();
            $referrer_cat_id = ( is_object( $referrer_cat ) ) ? $referrer_cat->term_id : false;

            next_post_smart("%link", "Next", TRUE);
            previous_post_smart("%link", "Previous", TRUE);

            the_title(); 
            the_content();

        endwhile; 

        $cat = get_category($referrer_cat);
        $count = $cat->count;
        $currentID = get_the_ID(); 
        $current = Get_Post_Number($currentID);

        printf(_( 'Post: %d of %d' ), $current, $count );

    endif;
?>

So, after adding a plugin and combining some code I have from a few places, I now have a nice piece of code that displays a Next and Previous link for posts that reside in multiple categories and I have the ability to display a post count and a total post count on single.php.

I hope this helps someone!

  • 1
    Don't abuse the php tags. In all your blocks you can use one opening php tag and one closing php tag, that is all. OK, oneor two more in the last block – Pieter Goosen Jul 21 '14 at 16:23
  • Sorry, I just wanted to get as close to the original code as possible, so people wouldn't get confused when they saw the final code. I'll keep that in mind for future posts :-) – Josh Rodgers Jul 21 '14 at 16:46
  • I took the libarty to clean up your code. I hope you find it in order. If not, just simply roll back my change – Pieter Goosen Jul 21 '14 at 16:57
  • 1
    Looks good, thanks for the update...I updated my original code to match the solution so people don't get confused :-) – Josh Rodgers Jul 21 '14 at 17:24

protected by fuxia Jul 28 '14 at 10:25

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.