Hi,
My theme has a default post-navigation widget that works quite well. The problem is that I have posts with a special taxonomy called "variant" and with the theme's navigation module, there doesn't appear to be a way to restrict the navigation to show only posts that share the same variant.
To get around this, I create two separate toolset views, one for "next" and one for "previous". These are identical views except that "previous" sorts posts by date descending and "next" sorts posts by date ascending.
For the most part, both of these views work correctly, except for one glitch that I cannot seem to resolve. I can describe this using the "next" view.
The toolset code seems to sort all my posts by date ascending as I expect, but I just want the view to select the first one in the list whose date is greater than the date of the currently displayed post. There does not appear to be a way to filter the result like this, so what happens is that, when there are no posts following the current post (i.e. with a more recent date), the view picks one from the beginning of the list, as if it is wrapping around.
I'm sure there must be a way of doing this so that, when I'm at the beginning of the list of posts of a particular variant, then the "Previous" link will be blank, and when I'm at the end of the list, the "Next" link will be blank.
Any suggestions you have would be most appreciated.
David McLeod
Hi David,
Thank you for contacting us and I'd be happy to assist.
To suggest some workaround, I'll need to see exactly how these two 'next' and 'previous' link views are set up.
Can you please share temporary admin login details along with the link to an example page where these links can be seen?
Note: Your next reply will be private and please make a complete backup copy, before sharing the access details.
regards,
Waqar
Thanks for writing back and for explaining the requirement through that screencast.
I'm afraid, the way the two views are being used, you can't possibly get the post navigation that you need.
In post navigation, a sequence or relativity exists between the next and previous links with respect to the current post. However, these two views will always point out the very first and the very last result items, as previous and last links.
(with the exception of when you'll be on the single page for the very first and last items, where they'll be ignored, due to view's setting 'Don't include current page in query result')
For post navigation links to cycle through only a particular taxonomy, you can register a custom shortcode:
add_shortcode('tax_based_navigation', 'tax_based_navigation_func');
function tax_based_navigation_func($atts) {
$taxonomy = $atts['taxonomy'];
$type = $atts['type'];
ob_start();
if ($type == 'previous') {
echo previous_post_link( '%link', '%title', true, ' ', $taxonomy );
}
elseif ($type == 'next') {
echo next_post_link( '%link', '%title', true, ' ', $taxonomy );
}
return ob_get_clean();
}
The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.
After that, you'll be able to use this shortcode like this.
For the previous post link, with respect to 'variant' taxonomy:
[tax_based_navigation type='previous' taxonomy='variant']
And, for the next post link, with respect to 'variant' taxonomy:
[tax_based_navigation type='next' taxonomy='variant']
That solution is exactly what I was looking for. And very easy to implement too, using the code snippet feature in the Settings.
Thanks!