Ask your WordPress questions! Pay money and get answers fast! (more info)

exclude category from custom 'prev-next navigation' not working WordPress

  • SOLVED

I'm trying to exclude a category (category id = 8) from a custom prev-next post navigation I have included in my wordpress site.

As [[LINK href="https://codex.wordpress.org/Function_Reference/get_adjacent_post"]]shown here[[/LINK]] I have added the '8' in the following but it is not excluding category #8 posts from the navigation.

// Don't print empty markup if there's nowhere to navigate.
$previous = ( is_attachment() ) ? get_post($post->post_parent) : get_adjacent_post(false, '8', true);
$next = get_adjacent_post(false, '8', true);

if (!$next && !$previous)
return;

$prevPost = get_previous_post(false, '8');
$prevthumbnail = get_the_post_thumbnail($prevPost->ID, 'thumbnail');
$nextPost = get_next_post(false, '8');
$nextthumbnail = get_the_post_thumbnail($nextPost->ID, 'thumbnail');


What am I doing wrong??

The site I'm using the above on is: http://wyrta.com/

<strong>P.s.</strong> Thumbnails seems to be excluded but the posts as mentioned above aren't, leading to wrong thumbnails being assigned to the excluded category posts.

Answers (3)

2015-08-05

Sébastien | French WordpressDesigner answers:

I think that : If a post is only in category 8 the code is ok, but if your post is in the category 8 AND in another category that doesn't work

if a post is ONLY in the category 8, is the post excluded from the navigation or not ?


AndrewL32 comments:

The post is only in one category Sebastien.


Sébastien | French WordpressDesigner comments:

ok but the category 8 is the children of an other category, isn't it ?


AndrewL32 comments:

No, it's not nested under any category.


Sébastien | French WordpressDesigner comments:

in this code :

$previous = ( is_attachment() ) ? get_post($post->post_parent) : get_adjacent_post(false, '8', true);

$next = get_adjacent_post(false, '8', true);


you use the same code for $previous and $next. You use :

get_adjacent_post(false, '8', true);

but if the third argument is "true" in both cases, you use the ID of the next post in both cases...
Why ? Is it really what you want ?!

you code must be :

$previous = ( is_attachment() ) ? get_post($post->post_parent) : get_adjacent_post(false, '8');

$next = get_adjacent_post(false, '8', true);


Sébastien | French WordpressDesigner comments:

if (!$next && !$previous)

return;


now this condition will be ok and your code will work


AndrewL32 comments:

I don't understand. I thought true was to prevent looping through the same category right?


Sébastien | French WordpressDesigner comments:

the first argument is to prevent looping through the same category
but the third argument is to choose if you want the next or the prev post



$prev_post = get_adjacent_post( true, '8', true);
OR
$prev_post = get_adjacent_post( true, '8');




$next_post = get_adjacent_post( true, '8', false);


AndrewL32 comments:

That makes sense but how come the prev-link and next-link are navigating correctly with the current code then Sebastien?

E.g. the prev and next link here is correct: http://wyrta.com/test-post-with-video-embedded/


Sébastien | French WordpressDesigner comments:

sorry, a mistake in my previous code, your code must be :

$previous = ( is_attachment() ) ? get_post($post->post_parent) : get_adjacent_post(false, '8');



$next = get_adjacent_post(false, '8', false);


Sébastien | French WordpressDesigner comments:

<blockquote>That makes sense but how come the prev-link and next-link are navigating correctly with the current code then Sebastien?

E.g. the prev and next link here is correct: http://wyrta.com/test-post-with-video-embedded/</blockquote>

your link are correct because this part of your code is use to display your links :

$prevPost = get_previous_post(false, '8');

$prevthumbnail = get_the_post_thumbnail($prevPost->ID, 'thumbnail');

$nextPost = get_next_post(false, '8');

$nextthumbnail = get_the_post_thumbnail($nextPost->ID, 'thumbnail');


and this part of your code is OK.

But this condition in your code :

if (!$next && !$previous)

return;



this condition is ALWAYS non-correct.


Sébastien | French WordpressDesigner comments:

could you try and tell me if it's ok ?


AndrewL32 comments:

So which part of the code should I change Sebastien?


Sébastien | French WordpressDesigner comments:

your original code is like that :

<?php

function wyrta_post_nav() {

global $post;

// Don't print empty markup if there's nowhere to navigate.

$previous = ( is_attachment() ) ? get_post($post->post_parent) : get_adjacent_post(false, '8', true);

$next = get_adjacent_post(false, '8', true);



if (!$next && !$previous)

return;



$prevPost = get_previous_post(false, '8');

$prevthumbnail = get_the_post_thumbnail($prevPost->ID, 'thumbnail');

$nextPost = get_next_post(false, '8');

$nextthumbnail = get_the_post_thumbnail($nextPost->ID, 'thumbnail');

?>

<nav class="navigation post-navigation" role="navigation">

<div class="container no-pad">

<div class="nav-links row">

<div id="prev-next">

<div class="previous-post col-xs-6 col-md-4"><?php previous_post_link( '%link', '<div class="meta-nav"><div class="row"><div class="col-md-6">'.$prevthumbnail.'</div>' . _x( '<div class="col-md-6"><div class="prevnext-title">Previous Article:</div>', 'Previous post link', 'wyrta' ) . '<div class="nav-title">%title</div> ' ); ?></div></div></div>

</div>

<div class="next-post col-xs-6 col-md-4 col-md-offset-4"><?php next_post_link( '%link', '<div class="meta-nav"><div class="row">' . _x( '<div class="col-md-6"><div class="prevnext-title">Next Article:</div>', 'Next post link', 'wyrta' ) . '<div class="nav-title">%title</div></div>' . '<div class="col-md-6">'.$nextthumbnail.'' ); ?></div></div></div>

</div><!-- .nav-links -->

</div>

</nav><!-- .navigation -->

<?php

}


you must change this lines

$previous = ( is_attachment() ) ? get_post($post->post_parent) : get_adjacent_post(false, '8', true);

$next = get_adjacent_post(false, '8', true);


into this lines :

$previous = ( is_attachment() ) ? get_post($post->post_parent) : get_adjacent_post(false, '8');

$next = get_adjacent_post(false, '8', false);


then your final code, the code you must use is like that :


function wyrta_post_nav() {

global $post;

// Don't print empty markup if there's nowhere to navigate.

$previous = ( is_attachment() ) ? get_post($post->post_parent) : get_adjacent_post(false, '8');

$next = get_adjacent_post(false, '8', false);



if (!$next && !$previous)

return;



$prevPost = get_previous_post(false, '8');

$prevthumbnail = get_the_post_thumbnail($prevPost->ID, 'thumbnail');

$nextPost = get_next_post(false, '8');

$nextthumbnail = get_the_post_thumbnail($nextPost->ID, 'thumbnail');

?>

<nav class="navigation post-navigation" role="navigation">

<div class="container no-pad">

<div class="nav-links row">

<div id="prev-next">

<div class="previous-post col-xs-6 col-md-4"><?php previous_post_link( '%link', '<div class="meta-nav"><div class="row"><div class="col-md-6">'.$prevthumbnail.'</div>' . _x( '<div class="col-md-6"><div class="prevnext-title">Previous Article:</div>', 'Previous post link', 'wyrta' ) . '<div class="nav-title">%title</div> ' ); ?></div></div></div>

</div>

<div class="next-post col-xs-6 col-md-4 col-md-offset-4"><?php next_post_link( '%link', '<div class="meta-nav"><div class="row">' . _x( '<div class="col-md-6"><div class="prevnext-title">Next Article:</div>', 'Next post link', 'wyrta' ) . '<div class="nav-title">%title</div></div>' . '<div class="col-md-6">'.$nextthumbnail.'' ); ?></div></div></div>

</div><!-- .nav-links -->

</div>

</nav><!-- .navigation -->

<?php

}


Sébastien | French WordpressDesigner comments:

your original code is :


function wyrta_post_nav() {

global $post;

// Don't print empty markup if there's nowhere to navigate.

$previous = ( is_attachment() ) ? get_post($post->post_parent) : get_adjacent_post(false, '8', true);

$next = get_adjacent_post(false, '8', true);



if (!$next && !$previous)

return;



$prevPost = get_previous_post(false, '8');

$prevthumbnail = get_the_post_thumbnail($prevPost->ID, 'thumbnail');

$nextPost = get_next_post(false, '8');

$nextthumbnail = get_the_post_thumbnail($nextPost->ID, 'thumbnail');

?>

<nav class="navigation post-navigation" role="navigation">

<div class="container no-pad">

<div class="nav-links row">

<div id="prev-next">

<div class="previous-post col-xs-6 col-md-4"><?php previous_post_link( '%link', '<div class="meta-nav"><div class="row"><div class="col-md-6">'.$prevthumbnail.'</div>' . _x( '<div class="col-md-6"><div class="prevnext-title">Previous Article:</div>', 'Previous post link', 'wyrta' ) . '<div class="nav-title">%title</div> ' ); ?></div></div></div>

</div>

<div class="next-post col-xs-6 col-md-4 col-md-offset-4"><?php next_post_link( '%link', '<div class="meta-nav"><div class="row">' . _x( '<div class="col-md-6"><div class="prevnext-title">Next Article:</div>', 'Next post link', 'wyrta' ) . '<div class="nav-title">%title</div></div>' . '<div class="col-md-6">'.$nextthumbnail.'' ); ?></div></div></div>

</div><!-- .nav-links -->

</div>

</nav><!-- .navigation -->

<?php

}


in this code your must change this line :

$previous = ( is_attachment() ) ? get_post($post->post_parent) : get_adjacent_post(false, '8', true);

$next = get_adjacent_post(false, '8', true);


into this lines :

$previous = ( is_attachment() ) ? get_post($post->post_parent) : get_adjacent_post(false, '8');

$next = get_adjacent_post(false, '8', false);


so the code you must use is finally this code :


function wyrta_post_nav() {

global $post;

// Don't print empty markup if there's nowhere to navigate.

$previous = ( is_attachment() ) ? get_post($post->post_parent) : get_adjacent_post(false, '8');

$next = get_adjacent_post(false, '8', false);



if (!$next && !$previous)

return;



$prevPost = get_previous_post(false, '8');

$prevthumbnail = get_the_post_thumbnail($prevPost->ID, 'thumbnail');

$nextPost = get_next_post(false, '8');

$nextthumbnail = get_the_post_thumbnail($nextPost->ID, 'thumbnail');

?>

<nav class="navigation post-navigation" role="navigation">

<div class="container no-pad">

<div class="nav-links row">

<div id="prev-next">

<div class="previous-post col-xs-6 col-md-4"><?php previous_post_link( '%link', '<div class="meta-nav"><div class="row"><div class="col-md-6">'.$prevthumbnail.'</div>' . _x( '<div class="col-md-6"><div class="prevnext-title">Previous Article:</div>', 'Previous post link', 'wyrta' ) . '<div class="nav-title">%title</div> ' ); ?></div></div></div>

</div>

<div class="next-post col-xs-6 col-md-4 col-md-offset-4"><?php next_post_link( '%link', '<div class="meta-nav"><div class="row">' . _x( '<div class="col-md-6"><div class="prevnext-title">Next Article:</div>', 'Next post link', 'wyrta' ) . '<div class="nav-title">%title</div></div>' . '<div class="col-md-6">'.$nextthumbnail.'' ); ?></div></div></div>

</div><!-- .nav-links -->

</div>

</nav><!-- .navigation -->

<?php

}


Sébastien | French WordpressDesigner comments:

sorry for the double post :-/


AndrewL32 comments:

Ok so I edited the function as you have instructed but only the thumbnails from category 8 are excluded not the posts.

If you go here: http://wyrta.com/jaintia-hills-producing-record-high-tomatoes/, the next link has a proper thumbnail but the post is from category 8. Clicking on it too leads to category 8 unfortunately.


Sébastien | French WordpressDesigner comments:

OK !! you don't use correctly the functions previous_post_link and next_post_link

Your final code must be like that


function wyrta_post_nav() {

global $post;

// Don't print empty markup if there's nowhere to navigate.

$previous = ( is_attachment() ) ? get_post($post->post_parent) : get_adjacent_post(false, '8');

$next = get_adjacent_post(false, '8', false);



if (!$next && !$previous)

return;



$prevPost = get_previous_post(false, '8');

$prevthumbnail = get_the_post_thumbnail($prevPost->ID, 'thumbnail');

$nextPost = get_next_post(false, '8');

$nextthumbnail = get_the_post_thumbnail($nextPost->ID, 'thumbnail');

?>

<nav class="navigation post-navigation" role="navigation">

<div class="container no-pad">

<div class="nav-links row">

<div id="prev-next">

<div class="previous-post col-xs-6 col-md-4"><?php previous_post_link( '%link', '<div class="meta-nav"><div class="row"><div class="col-md-6">'.$prevthumbnail.'</div>' . _x( '<div class="col-md-6"><div class="prevnext-title">Previous Article:</div>', 'Previous post link', 'wyrta' ) . '<div class="nav-title">%title</div> ', false, '8' ); ?></div></div></div>

</div>

<div class="next-post col-xs-6 col-md-4 col-md-offset-4"><?php next_post_link( '%link', '<div class="meta-nav"><div class="row">' . _x( '<div class="col-md-6"><div class="prevnext-title">Next Article:</div>', 'Next post link', 'wyrta' ) . '<div class="nav-title">%title</div></div>' . '<div class="col-md-6">'.$nextthumbnail.'', false, '8' ); ?></div></div></div>

</div><!-- .nav-links -->

</div>

</nav><!-- .navigation -->

<?php

}


Sébastien | French WordpressDesigner comments:

I explain to you :

to display the previous post you use this code :

<?php previous_post_link( '%link', '<div class="meta-nav"><div class="row"><div class="col-md-6">'.$prevthumbnail.'</div>' . _x( '<div class="col-md-6"><div class="prevnext-title">Previous Article:</div>', 'Previous post link', 'wyrta' ) . '<div class="nav-title">%title</div> ' ); ?>

here your first argument is '%link'
your second argument displays the text and the thumbnail of your link : '<div class="meta-nav"><div class="row"><div class="col-md-6">'.$prevthumbnail.'</div>' . _x( '<div class="col-md-6"><div class="prevnext-title">Previous Article:</div>', 'Previous post link', 'wyrta' ) . '<div class="nav-title">%title</div> '

but you have no argument to select the excluded categories

so you must use the third argument in_same_term (false) and the fourth argument excluded_terms ('8')

so your final function is
<?php previous_post_link( '%link', '<div class="meta-nav"><div class="row"><div class="col-md-6">'.$prevthumbnail.'</div>' . _x( '<div class="col-md-6"><div class="prevnext-title">Previous Article:</div>', 'Previous post link', 'wyrta' ) . '<div class="nav-title">%title</div> ', false, '8' ); ?>


same thing with next_post_link


mre about this arguments : [[LINK href="https://codex.wordpress.org/Function_Reference/previous_post_link"]]https://codex.wordpress.org/Function_Reference/previous_post_link[[/LINK]]



AndrewL32 comments:

Sebastien, your latest code snippet worked!! Thank you so much man!! Can I please know what was the reason it didn't work initially?

2015-08-05

Shoeb mirza answers:

It is mentioning taxonomy_slug
Can you try '-8'

https://css-tricks.com/snippets/wordpress/remove-specific-categories-from-the-loop/


AndrewL32 comments:

I changed the 8 to '-8' and it still didn't work Shoeb :(

2015-08-05

Andrea P answers:

it seems that you have to pass an array to the get_adjacent_post

could you try changing every instance of that function in this way

get_adjacent_post(false, array('8'), true);


Andrea P comments:

and now that I am thinking, probably you have to do the same with get_previous_post and get_next_post


Andrea P comments:

nope sorry, the codec says only string for those two, but the get_adjacent_post asks for an array as well.
and probably it needs and integer within the array, like so:


get_adjacent_post(false, array(8), true);


AndrewL32 comments:

So I'll change all the `8` to array(8) Andrea?


AndrewL32 comments:

I changed the 8 to array (8) and the posts are still showing Andrea :( Weirdly enough, the thumbnails from category 8 are not showing as I wanted though with 8 or -8 or array(8). Only the posts are showing with thumbnails from other category posts.


Andrea P comments:

mmm this is quite weird indeed..

could you link me the precise page where you are displaying this?
is that all the code you have to display it?


AndrewL32 comments:

Here is the function added in functions.php:

function wyrta_post_nav() {
global $post;
// Don't print empty markup if there's nowhere to navigate.
$previous = ( is_attachment() ) ? get_post($post->post_parent) : get_adjacent_post(false, '8', true);
$next = get_adjacent_post(false, '8', true);

if (!$next && !$previous)
return;

$prevPost = get_previous_post(false, '8');
$prevthumbnail = get_the_post_thumbnail($prevPost->ID, 'thumbnail');
$nextPost = get_next_post(false, '8');
$nextthumbnail = get_the_post_thumbnail($nextPost->ID, 'thumbnail');
?>
<nav class="navigation post-navigation" role="navigation">
<div class="container no-pad">
<div class="nav-links row">
<div id="prev-next">
<div class="previous-post col-xs-6 col-md-4"><?php previous_post_link( '%link', '<div class="meta-nav"><div class="row"><div class="col-md-6">'.$prevthumbnail.'</div>' . _x( '<div class="col-md-6"><div class="prevnext-title">Previous Article:</div>', 'Previous post link', 'wyrta' ) . '<div class="nav-title">%title</div> ' ); ?></div></div></div>
</div>
<div class="next-post col-xs-6 col-md-4 col-md-offset-4"><?php next_post_link( '%link', '<div class="meta-nav"><div class="row">' . _x( '<div class="col-md-6"><div class="prevnext-title">Next Article:</div>', 'Next post link', 'wyrta' ) . '<div class="nav-title">%title</div></div>' . '<div class="col-md-6">'.$nextthumbnail.'' ); ?></div></div></div>
</div><!-- .nav-links -->
</div>
</nav><!-- .navigation -->
<?php
}


And I add this at the end of single.php :

<?php wyrta_post_nav(); ?>

Here is a post where the <strong>previous</strong> is linked to a category 8 post: http://wyrta.com/chah-jied-ka-chnong-jalasiaw-kam-ka-nicra-village-ha-jaintia-hills/