-1

I'm trying to use the Smarter Navigation plugin to work so that when I click on an individual post from the category page, the single post navigation only goes to the next post in the same category. I am using the plugin because the default WP navigation doesn't take into account the referring link when a post has multiple categories.

This is the code for the link to the previous post in single.php:

<?php 
    $prev_post = get_previous_post(TRUE,'all','category');
    if(!empty($prev_post)):
        echo 'not empty'; 
        previous_post_smart();
    else:
        echo 'empty';
    endif; 
?>

This is what is happening, taking into account these posts and category arrangements

Category-ALL: 4th-post, 3rd-post, 2nd-post, 1st-post,

Category-IMAGE: 4th-post, 2nd-post

Category-VIDEO: 3rd-post, 1st-post

With the above code in the single.php template for the 2nd-post, if I click on the link for the 2nd-post from the Category-IMAGE page, it will echo not empty but the previous_post_smart() function will return nothing because there is no previous post in the IMAGE category.

If I understand correctly get_previous_post(TRUE,'all','category') is saying, "get the previous post in the same category excluding the category all, but I think what is happening is that it is including the category all.

I say this because the same is true for the reverse; for the 3rd-post, referred from the Category-VIDEO page, with the get_next_post() function with the same parameters it also returns (!empty)

1

Figured it out :) It ends up the Smarter Navigation plugin has a few other functions that I missed:

https://plugins.trac.wordpress.org/browser/smarter-navigation/trunk/template-tags.php

I used get_adjacent_id_smart() like so:

<?php
    $prev_post = get_adjacent_id_smart(true);
    if(!empty($prev_post)):
        echo 'not empty'; 
        previous_post_smart();
    else:
        echo 'empty';
    endif; 
?>

And I got the behavior I was looking for, don't know why the default WP function was not working according to the codex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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