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

1
  • I would believe that a post is only assigned to one category then Commented Jul 16, 2014 at 6:11

1 Answer 1

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!

4
  • 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 Commented Jul 21, 2014 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 :-) Commented Jul 21, 2014 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 Commented Jul 21, 2014 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 :-) Commented Jul 21, 2014 at 17:24

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