Community Forums › Forums › Archived Forums › General Discussion › Exclude blog category from single post navigation
Tagged: post navigation category exclude
- This topic has 11 replies, 3 voices, and was last updated 9 years, 7 months ago by Brad Dalton.
-
AuthorPosts
-
August 22, 2014 at 8:19 am #120472carrieoke13Participant
Hi,
Is there a way to exclude a blog category from single post navigation (previous/next buttons at the bottom of the post)?Thanks!
http://mylifeinknitwear.comAugust 22, 2014 at 8:55 am #120476carrieoke13ParticipantI'm adding some more info in case it is helpful.
I'm already excluding this category from the blog page using this code:
/** Exclude certain category from blog page */ add_action( 'pre_get_posts', 'be_exclude_category_from_blog' ); function be_exclude_category_from_blog( $query ) { if( $query->is_main_query() && $query->is_home() ) { $query->set( 'cat', '-44' ); } }
but I'd like for it not to appear in the previous/post nav, or the monthly archive pages. I basically want the whole category to be separate from the blog.
August 22, 2014 at 10:02 am #120488Ren VenturaMemberIf you're trying to keep eveything in this category separate, perhaps a custom post type might be ideal. Just a suggestion.
This should do the trick, though:
remove_action( 'genesis_entry_footer', 'genesis_prev_next_post_nav' );
http://my.studiopress.com/snippets/entry-content/
Web & Software Developer & Blogger | RenVentura.com | Follow Me on Twitter @CLE_Ren
August 22, 2014 at 11:53 am #120514carrieoke13ParticipantThanks, Ren. I thought a custom post type might be the better solution.
If I was going to keep this as a blog category, are you saying I would put that snippet in my functions.php? how will it know which category to exclude? I want to keep the post navigation on regular posts.
Thanks!
August 22, 2014 at 12:11 pm #120519Ren VenturaMemberYes, it would go in your functions.php and you'd use it with the appropriate conditionals. For example:
add_action( 'genesis_entry_footer', 'rv_remove_post_nav_cat', 999 ); function rv_remove_post_nav_cat() { if( has_category( 44 ) ) { remove_action( 'genesis_entry_footer', 'genesis_prev_next_post_nav' ); } }
You could also find where your child theme is adding the genesis_prev_next_post_nav and add
if( !has_category( 44 ) )
to only add the single post navigation if the post does NOT have a category ID of 44. Personally, this is the route I'd take because it's cleaner.
Web & Software Developer & Blogger | RenVentura.com | Follow Me on Twitter @CLE_Ren
August 22, 2014 at 12:15 pm #120521carrieoke13Participantwith the solutionn you're suggesting, post nav is removed from excluded categories, but those posts still remain in the navigation. as in, the post nav on the posts before and after the posts in those categories will still have navlinks pointing to those posts. (like, post 1 is in the blog category, post is in the patterns category, which is the one I want excluded. So Post 1 still has post nav, and the "next" link goes to Post 2.) so it isn't exactly accomplishing what I want because I don't want any links to the excluded posts to be on the post nav. am I understanding it wrong?
August 22, 2014 at 12:32 pm #120522Ren VenturaMemberI see what you're saying: you want to remove the posts completely from the entire post navigation rather than remove the post navigation from the posts. I haven't tested the code but that would be logical. You may want to go with a custom post type because this is breaking the flow of your normal blog posts and seems like a pretty big hack. If someone else can chime in with a solution I'm not thinking of, awesome. My suggestion, though, is to create a custom post type. Is this something you're comfortable doing?
Web & Software Developer & Blogger | RenVentura.com | Follow Me on Twitter @CLE_Ren
August 22, 2014 at 12:43 pm #120523Brad DaltonParticipantTry the 4th parameter for both the next and previous post links
excluded_terms
(string/array) (optional) Array or a comma-separated list of numeric terms IDs from which the next post should not be listed. For example array(1, 5) or '1,5'. This argument used to accept a list of IDs separated by 'and', this was deprecated in WordPress 3.3
Default: None<?php next_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ); ?>
This is only the idea. The code will need to be written.
August 22, 2014 at 1:01 pm #120525carrieoke13ParticipantOkay I'm trying this:
/*add previous and next post links*/ add_action( 'genesis_entry_footer', 'custom_single_post_nav', 12 ); /** * @author Brad Dalton * @example http://wpsites.net/web-design/different-ways-to-add-post-navigation-pagination-in-genesis/ * @copyright 2014 WP Sites */ function custom_single_post_nav() { if ( ! is_singular( 'post' ) ) return; echo '<div class="pagination-previous alignleft">'; echo '<<'; previous_post_link('%link', ' %title', FALSE, '44'); echo '</div>'; echo '<div class="pagination-next alignright">'; next_post_link('%link', '%title ', FALSE, '44'); echo '>>'; echo '</div>'; }
(which is your code for pagination, Brad!) and tried to add the parameter. It seems to be excluding the category; but it's doing something weird with the first post on the blog.
So if you look, the default previous/next links are still showing on the blog: http://mylifeinknitwear.com/the-knit-generation/
and the default nav is going to one post, the custom nav I added is going to a different post that is a couple of posts away.
August 22, 2014 at 1:03 pm #120526carrieoke13ParticipantSo I was wrong! I turned off the cache plugin and the right links are showing now!
August 22, 2014 at 1:05 pm #120527carrieoke13ParticipantAaaaand it's working!
This post has one of the posts in the excluded category before it:
http://mylifeinknitwear.com/sands-end/
notice the difference in the two navs under the post.
Thanks so much, Brad!
August 22, 2014 at 1:08 pm #120528Brad DaltonParticipant -
AuthorPosts
- The topic ‘Exclude blog category from single post navigation’ is closed to new replies.