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 thenext_post_link
andprevious_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:
-
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…
-
I get my two referrer variables from the
Smarter Navigation
plugin:referrer_cat
andreferrer_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;
- 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 ofnew 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!