Community Forums › Forums › Archived Forums › Design Tips and Tricks › Anyone know why this in_category code isn't working?
Tagged: category
- This topic has 7 replies, 3 voices, and was last updated 9 years, 2 months ago by desirevo.
-
AuthorPosts
-
February 4, 2015 at 1:47 pm #139683desirevoMember
Hi guys,
I'm attempting to remove the content of two specific certain categories from the index page. I've created index.php, and I'm using the following code on it:
if (in_category (array( 'Update', 'Guides')) ) { remove_action( 'genesis_entry_content', 'genesis_do_post_content' ); }
Is this incorrect? Is there a way of executing this that i'm missing? It's not returning any errors, it's just not having any effect.
Apologies for not providing a live site, I'm currently working locally.
Thanks.
February 4, 2015 at 1:48 pm #139684PorterParticipantHave you tried tossing an echo in there to see if it prints to the page? It MAY have something to do with needing to use front-page.php instead of index.php, though I'm not sure.
February 4, 2015 at 1:53 pm #139685desirevoMemberThanks for the suggestion. How would I write that echo in order to achieve that?
February 4, 2015 at 2:54 pm #139690CarloMemberHi desirevo. Your code is way off, unfortunately! If you like, I can give you the correct code, or you could just go to your Genesis theme settings and the 'Blog Page Template' section and exclude the category IDs there.
February 4, 2015 at 2:55 pm #139691desirevoMemberHi Carlo. Sure, if you could point out what I'm doing wrong, then that'd be great. I'd just like to learn from whatever mistake I've made, and ensure I approach it correctly the next time.
February 4, 2015 at 5:33 pm #139733PorterParticipantAs far as echo goes, you can use it print text out like so:
echo("Testing to see if this code is read");
That'll print the text in quotes on the page, or not, if it's not working. You can use it to print variables, or anything, which is great for debugging.
February 6, 2015 at 3:45 am #139927CarloMemberPorter, I'm not sure how the echo command is relevant here.
Desirevo, did you try the suggestion I gave you? Try that first, and if it doesn't work, you could try adding this code to your theme functions:
add_filter( 'pre_get_posts', function( $query ) { if ( $query->is_home() ) { $query->set( 'cat', '-2,-3' ); } return $query; } );
Replace 2 and 3 with the IDs of your categories you want to exclude.
However, I'm not 100% sure I understand which page you want to exclude the categories from, so the code might not work for you. Let me know where it gets you.
February 6, 2015 at 4:24 am #139931desirevoMemberHi Carlo, thanks for the suggestion. I think there may be a little confusion as to what I'm trying to do.
I'm not trying to exclude a category as a whole, what I'm trying to do is exclude the entry content for specific categories from the index/home/blog page.
For instance, let's say I have categories Apple, Banana, and Cherry.
I want them all to appear on the index page, but I want the Apple and Banana categories to display entry content on the index page, and I don't want the Cherry category to do that.
Does this make sense?
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.