Community Forums › Forums › Archived Forums › Design Tips and Tricks › How to hide blog posts from a specific category on home page?
- This topic has 15 replies, 2 voices, and was last updated 11 years, 7 months ago by maheshone.
-
AuthorPosts
-
May 24, 2013 at 10:59 pm #42568maheshoneMember
Hi,
Can anyone tell me how to hide blog posts from a specific category or categories on the home page?
For example, My blog is http://www.minterest.com/ and on the homepage all you see is daily mashups so I want to hide all blog posts from the categoryhttp://www.minterest.com/category/minteresting/.
~ Mahesh
Twitter — @maheshone
Blog — http://www.minterest.com/
vCard — http://www.maheshone.com/May 25, 2013 at 2:18 am #42577Brad DaltonParticipantMay 25, 2013 at 5:39 am #42582maheshoneMemberI must place the code
<?php if ( is_home() ) { query_posts( 'cat=-1' ); } ?>here...
`genesis_before` Hook
This hook executes immediately after the opening `` tag.Right?
Twitter — @maheshone
Blog — http://www.minterest.com/
vCard — http://www.maheshone.com/May 25, 2013 at 5:40 am #42583maheshoneMemberHi Brad,
I think it will solve my issue... I want to try Option 2 (that's by using hooks).
But can you please tell me which hook should I use?
More specifically... where exactly should I place the code
<?php if ( is_home() ) { query_posts( 'cat=-1' ); } ?>
before the closing </head> tag, before the closing </body> tag, after the opening <body> tag. or somewhere else?
Thanks!
Twitter — @maheshone
Blog — http://www.minterest.com/
vCard — http://www.maheshone.com/May 26, 2013 at 1:48 pm #42707maheshoneMemberBUMP.... Can some one reply quickly? I'm still waiting...
Please tell me which hook should I use?
The following one?
`genesis_before` Hook
This hook executes immediately after the opening `` tag.
Twitter — @maheshone
Blog — http://www.minterest.com/
vCard — http://www.maheshone.com/May 27, 2013 at 11:31 pm #42873maheshoneMemberSomeone update this?
Twitter — @maheshone
Blog — http://www.minterest.com/
vCard — http://www.maheshone.com/May 28, 2013 at 8:27 am #42909maheshoneMemberHi there,
I used the code "<?php if ( is_home() ) { query_posts( ‘cat=-1′ ); } ?>"
And placed in the genesis_before Hook and it says this hook executes immediately after the opening <body> tag.
And I noticed that the blog posts from that particular category was not showing on the home page. But... I was unable to browse the pages... it was showing the homepage posts itself when I click on Page 2, 3, etc.
Twitter — @maheshone
Blog — http://www.minterest.com/
vCard — http://www.maheshone.com/May 30, 2013 at 9:56 am #43218maheshoneMemberCan any one help? It's been several days and there's no support yet.
Twitter — @maheshone
Blog — http://www.minterest.com/
vCard — http://www.maheshone.com/May 30, 2013 at 6:22 pm #43275Brad DaltonParticipantMay 30, 2013 at 9:29 pm #43295maheshoneMemberYes, I'm now using a plugin temporarily... But I prefer to use a hook or coding manually... can you help that way? Or the hooks don't work?
Twitter — @maheshone
Blog — http://www.minterest.com/
vCard — http://www.maheshone.com/May 30, 2013 at 9:57 pm #43305Brad DaltonParticipantAll the code in the tutorial provides different methods to hide specific categories from your home or blog page.
You will need to modify the code with the category i.d's for your specific needs.
The code I have provided is tested and works in your child themes functions.php file rather than Simple Hooks.
May 30, 2013 at 10:11 pm #43308maheshoneMemberHi,
I'm bad with coding... can anyone help me with this?
I have pasted the child theme functions.php here
https://docs.google.com/document/d/1fX_WQz_WSOwSH37Txmf4pLTyvMKslrQsnyFZaTaVQi4/edit [Anyone Can View & Edit This File]
Can anyone place the necessary codes (pasted below) at the right place? I want to remove only one category.
//Code for one category
<?php if ( is_home() ) { query_posts( 'cat=-1' ); } ?>
Twitter — @maheshone
Blog — http://www.minterest.com/
vCard — http://www.maheshone.com/May 30, 2013 at 10:28 pm #43309Brad DaltonParticipantMay 30, 2013 at 10:53 pm #43313maheshoneMemberHi,
I tried that and it gave the error...
Parse error: syntax error, unexpected T_STRING in /home/interest/public_html/wp-content/themes/focus/functions.php on line 36
I used the following code...
/** Remove a category from home page*/
add_action( 'pre_get_posts', 'remove_posts_in_category_on_blog' );
function remove_posts_in_category_on_blog( $query ) {global $wp_the_query;
if( $wp_the_query === $query && $query->is_home() ) {
$query->set( 'cat', '-311' );
}}
Twitter — @maheshone
Blog — http://www.minterest.com/
vCard — http://www.maheshone.com/May 30, 2013 at 11:10 pm #43318Brad DaltonParticipantHello @maheshone
Please copy the code using the view raw link and paste it at the end of your child themes functions.php file using a text editor like Notepad++.
All my code works because its all tested.
Just tested it again locally and it works without errors.
Code not displaying? Grab it on Github.
This code was tested on Genesis and excludes all posts from displaying on the home page which are in the category with i.d 18.
You will need to change the 18 to your own category i.d.
May 30, 2013 at 11:26 pm #43320maheshoneMemberFinally, it worked!
Thanks a ton! 🙂
Twitter — @maheshone
Blog — http://www.minterest.com/
vCard — http://www.maheshone.com/ -
AuthorPosts
- The topic ‘How to hide blog posts from a specific category on home page?’ is closed to new replies.