Community Forums › Forums › Archived Forums › General Discussion › Exclude category from blog template
Tagged: blog page template, exclude category, pre_get_posts
- This topic has 6 replies, 3 voices, and was last updated 9 years ago by
Brad Dalton.
-
AuthorPosts
-
February 13, 2017 at 11:38 am #201050
Deluxe Designs
MemberI have entered the cat id I want to exclude in genesis settings and nothing happened. I also used this code to no avail:
add_action( 'pre_get_posts', 'be_exclude_category_from_blog' ); /** * Exclude Category from Blog * * @author Bill Erickson * @link http://www.billerickson.net/customize-the-wordpress-query/ * @param object $query data * */ function be_exclude_category_from_blog( $query ) { if( $query->is_main_query() && $query->is_home() ) { $query->set( 'cat', '-14' ); } }Where 14 is the category id I do not want to display on the blog page. The category happens to be a parent category (I do not want that category nor its subcats to display) so just as a test, I tried to also exclude the subcategory id's as well and it didn't matter. I also changed the query to is_page ( 42 ) where 42 is the id of the blog page. None of these worked. This is a customized version of foodie pro that I am working on.
http://thelittleclementine.com/blog/February 13, 2017 at 11:54 am #201051Victor Font
ModeratorThe $query->is_home() is causing the action to fail. This function returns true only on the page you set as the “Posts page” in the WordPress settings. Change that condition to is_page('104') and it should work.
Regards,
Victor
https://victorfont.com/
Call us toll free: 844-VIC-FONT (842-3668)
Have you requested your free website audit yet?February 13, 2017 at 12:20 pm #201053Deluxe Designs
MemberSorry I meant to say 104 (not 42) in my original post but I have tried that as well - and just tried it again to give the benefit of the doubt - and it still did not work.
February 13, 2017 at 12:39 pm #201058Deluxe Designs
MemberI think it is something to do with a plugin or the site. I tried the same thing on the demo site for this and it worked. I'm not sure what the difference would be.
February 13, 2017 at 12:39 pm #201059Victor Font
ModeratorThen forget the function for now and use the built-in Genesis query_args method: https://victorfont.com/use-query_args-filter-wordpress-posts/
Regards,
Victor
https://victorfont.com/
Call us toll free: 844-VIC-FONT (842-3668)
Have you requested your free website audit yet?February 13, 2017 at 12:44 pm #201060Deluxe Designs
MemberThat time it worked. Thank you; I hadn't seen that before.
February 13, 2017 at 1:12 pm #201062Brad Dalton
Participantis_home(), is_page('blog'), is_page('id') and is_page_template('page_blog.php') with pre_get_posts will not check if the Genesis page template is loaded.
-
AuthorPosts
- The topic ‘Exclude category from blog template’ is closed to new replies.