Paging Through Sub Category Posts


I ran into a little hiccup in my WordPress development the other day. Paging through a single sub category.

See, the built in function of WordPress allows only the paging through of a category. So, when you’re in, say, the Portfolio category and you click on, say, the sub category of Illustrations, the link “Next Post” take you to the next Portfolio post in line, not the necessarily the next “Illustration” post in line. And it’s alphabetical.

So, we developed a little function that:

Finds the parent category

Eliminates the parent category from the query

Lists the sub category

Organizes them in line by ascending or descending order, depending on whether you’re hitting the Previous or Next button/link.

Word of Warning

If you’re planning on using this extensively, and I think you should, don’t check “Portfolio” AND “[Sub Category Name]” in the post categories when you’re writing and publishing your post. This will, when the sub category names are named something alphabetically AFTER the word Portfolio, confuse the function and render it useless to the sub categories under those names.

Make Sense?

Ok then. Let’s get down to it.

First, we need to get the sub category:

function get_the_subcategory()
{
$categories = get_the_category();

// get the sub category if we have them
foreach ($categories as $cat)
{
$parent = $cat->category_parent;

if ($parent != 0 )
{
$sub_cat_ID = $cat->cat_ID;
}
}

if (!$sub_cat_ID)
{
return false;
}
else
{
return $sub_cat_ID;
}
}

What this is doing is extracting the sub category ID in a variable so we can call it later. In a nut shell.

Next, we need to create a variable for the links. We need to get the permalink to the NEXT post in the sub category. First, create a variable based on the previous function. Then, query those posts so they are all together, organized. Then order them accordingly. Here we ordered them by post date and, for the NEXT button, we ordered them in Ascending order:

function get_next_subcategory_post_link()
{
$cat_ID = get_the_subcategory();

if($cat_ID != false)
{
$args = array(
‘numberposts’     => 1000,
‘category’        => $cat_ID,
‘orderby’         => ‘post_date’,
‘order’           => ‘ASC’ );

$list = get_posts($args);

$current = false;

foreach($list as $post)
{
if($current == true)
{
return get_permalink($post->ID);
}

if($post->ID == get_the_ID())
{
$current = true;
}
else
{
}
}
}
else
{
return “#error”;
}
}

Now, we’ll do pretty much the same thing for the Previous Posts link, except we’re getting the posts in Descending order:

function get_previous_subcategory_post_link()
{
$cat_ID = get_the_subcategory();
$args = array(
‘numberposts’     => 1000,
‘category’        => $cat_ID,
‘orderby’         => ‘post_date’,
‘order’           => ‘DESC’ );

$list = get_posts($args);

$current = false;

foreach($list as $post)
{
if($current == true)
{
return get_permalink($post->ID);
}

if($post->ID == get_the_ID())
{
$current = true;
}
else
{
}
}
}

Ok, now to call this into play. When you’re in your single.php file, simply call these functions like so (I’ve added in my CSS, you can do this however you want. I went with a generic setup for simplicity on the site. Seemed to work aesthetically as well.

<div>
<?php
$previous = get_previous_subcategory_post_link();
$next = get_next_subcategory_post_link();

if($previous != false) { ?>
<div style=”color: #036″><a href=”<?php echo $previous; ?>”>Previous Project</a><?php //previous_post_link(‘%link’, ‘Previous Project’, $sub_cat_ID, ’5′); ?></div>
<?php } ?>
<?php if($next != false) { ?>
<div style=”color: #036″><a href=”<?php echo $next; ?>”>Next Project</a><?php //echo ‘<a href=”‘ . $sub_cat_url . ‘”>View More from&nbsp;’ . $sub_cat_name . ‘</a>’; ?></div>
<?php } else {
echo ‘<div style=”color: #036″><a href=”‘[Your Portfolio Page URL]‘”>Back To Main Portfolio Page</a></div>’;
} ?>
</div>

And there you have it. To see this in action, go to jsclark.com.