Archived topic
Exclude a category from "Previous Next articles"
30 replies · Started by Elia on May 15, 2019
Hi Tom,
Thanks for the reply! I added all the information in the first post, please take a look.
Also please be careful because it's a live website
Any chance you can share the functions you're using in here? I wasn't able to login with the password you provided.
Let me know :)
I put all the information in the first page but your website changed it to something unreadable..
This time I only put a link to a code. Please have a look my first post here
I only changed 2 things:
1) Change "Category C" to the actual name of the category (not sure if this is correct because usually we used the ID).
2) Change the ID from 10 to 40
Thanks a lot
Maybe try the ID in has_category() instead of the name.
This doesn't work sorry
What if the ID doesn't have quotations around them?
Technically it should work, so there must be something conflicting somewhere. Can you link me to a post where the code should be working?
I tried the ID without quations - still no result.
Also I tried to apply the same code on the dev website and see this issue:
https://gyazo.com/07fde5aaf4682649c2de2561d5ed9143
Do you have a chance to apply and test the code on your side?
I've been playing with this and unfortunately it's incredibly complex. I played with the code David provided in the other topic, but I'm still not sure it's working:
<div id="custom-post-nav" class="post-nav-grid">
<?php global $post;
$post = get_queried_object();
$post_type = get_post_type();
if ( has_category( 4 ) ) {
$prevPost = get_previous_post( true, array( 4 ) );
} else {
$prevPost = get_previous_post();
}
if($prevPost) {
$args = array(
'posts_per_page' => 1,
'include' => $prevPost->ID,
'post_type' => $post_type
);
$prevPost = get_posts($args);
foreach ($prevPost as $post) {
setup_postdata($post);
?>
<a class="post-previous" href="<?php the_permalink(); ?>">
<span class="nav-icon">❮</span><span class="post-nav-label">Previous Article</span>
<h5 class="post-nav-title"><?php the_title(); ?></h5>
</a>
<?php
wp_reset_postdata();
} //end foreach
} // end if
if ( has_category( 4 ) ) {
$nextPost = get_next_post( true, array( 4 ) );
} else {
$nextPost = get_next_post(false);
}
if($nextPost) {
$args = array(
'posts_per_page' => 1,
'include' => $nextPost->ID,
'post_type' => $post_type
);
$nextPost = get_posts($args);
foreach ($nextPost as $post) {
setup_postdata($post);
?>
<a class="post-next" href="<?php the_permalink(); ?>">
<span class="post-nav-label">Next Article</span><span class="nav-icon">❯</span>
<h5 class="post-nav-title"><?php the_title(); ?></h5>
</a>
<?php
wp_reset_postdata(false);
} //end foreach
} // end if
?>
</div>
It's worth trying. Just make sure to remove the other filters we've added in this topic.
Hi Tom!
Thanks a lot!
I've applied the code from this topic https://generatepress.com/forums/topic/change-the-style-for-the-previous-next-section/page/2/#post-916642
The Previous/Next section is looking nice and working!
The only task that is not working yet is to exclude the category from the Previous/Next section.
If my category ID is 40 do I have to change 4 to 40 in the code above? I tried both and they don't work
Ah yes sorry - you'll need to update it to 40. I was afraid that would be the case, it's pretty complex unfortunately.
I'm only on emails for the weekend, but I can play with it a little more on Monday when I'm back at the coding computer.
Hi Tom, thanks a lot!
Sad to hear that it’s a difficult task. Could we perhaps tackle it differently?
We now have 2 main things on the website:
1) A blog about variety topics
2) Recipes
The idea is to somehow isolate second Recipes from the blog
Have you explored using a custom post type for the recipes? That way you would have "Posts" for your variety of topics, and "Recipes" for your recipes.
Let me know :)
Thanks Tom!
No I didn't try the custom post type.
Where can I try this? Would this resolve our issue with the navigation?
Can I use it without installing any plugins? Because we already have a lot of plugins which affects the speed of our website
The easiest way to use a plugin (this is what I used) - should have minimal impact on your site:
https://en-ca.wordpress.org/plugins/custom-post-type-ui/
Otherwise take a look at this article:
https://www.wpbeginner.com/wp-tutorials/how-to-create-custom-post-types-in-wordpress/
You can find lots of result by Google "How to create custom post type WordPress".
Hi Leo,
Seems that we would have to install some plugins. Which is not desired at all.
I'll wait for Tom's reply then.
Tom, please let me know if you have any success with excluding the category. If it's too difficult then let me know
Many thanks