- This topic has 15 replies, 2 voices, and was last updated 5 years, 5 months ago by Tom.
-
AuthorPosts
-
November 2, 2018 at 7:07 am #717223Carson
I am trying to disable post meta for certain categories.
Meaning, if category ID is “x”, I want no date, no name or any other meta to show up on the post itself as well as next to the preview in cetegory pages/wp show posts.add_filter( 'option_generate_blog_settings', 'lh_custom_author_page_settings' ); function lh_custom_author_page_settings( $options ) { $author = get_the_author_meta( 'ID' ); if ( 3 === $author ) { $options['author'] = false; $options['single_author'] = false; $options['date'] = false; $options['categories'] = false; $options['tags'] = false; } return $options; }
I am using the above code to hide meta based on authors and I tried to alter it so it works for categories as well. Didn’t succeed so far. Should I also be worried about the code snippets interfering with each other?
November 2, 2018 at 3:24 pm #717640TomLead DeveloperLead DeveloperHi there,
Give this a shot:
add_filter( 'option_generate_blog_settings', 'lh_custom_author_page_settings' ); function lh_custom_author_page_settings( $options ) { $author = get_the_author_meta( 'ID' ); if ( 3 === $author || has_category( array( 10, 20, 25 ) ) ) { $options['author'] = false; $options['single_author'] = false; $options['date'] = false; $options['categories'] = false; $options['tags'] = false; } return $options; }
10, 20 and 25 being the IDs you’re targeting.
I wouldn’t worry about code snippets conflicting, but it’s not impossible depending on the kind of snippet.
November 3, 2018 at 3:47 am #717878CarsonThanks a lot for your reply!
I was actually hoping to have a separate snippet for the categories, as I might only want to hide the date for some categories while showing the author etc. Is that possible?
November 3, 2018 at 9:23 am #718108TomLead DeveloperLead DeveloperFor sure:
add_filter( 'option_generate_blog_settings', function( $options ) { if ( has_category( array( 10, 20, 25 ) ) ) { $options['author'] = false; $options['single_author'] = false; $options['date'] = false; $options['categories'] = false; $options['tags'] = false; } return $options; } );
November 4, 2018 at 7:37 am #718646CarsonI tried this on several installs and it doesn’t seem to work. Is this an issue on my side?
November 4, 2018 at 9:25 am #718773TomLead DeveloperLead DeveloperCan you link me to a page that has a post with one of those categories?
November 6, 2018 at 8:39 am #720523CarsonUnfortunately I can’t share any of the sites on here. However, if the snippet works for you I have to do some testing and see why it doesn’t on my installs.
November 6, 2018 at 6:31 pm #720862TomLead DeveloperLead DeveloperAre you wanting those elements to be removed on the actual category pages, or within the single posts if they belong to a category?
November 7, 2018 at 3:27 am #721072Carsonwithin the single posts if they belong to a category
This is what I need. Sorry if I haven’t been clear before.
November 7, 2018 at 8:38 am #721411TomLead DeveloperLead DeveloperTry this:
add_action( 'wp', function() { if ( has_category( array( 10, 20, 25 ) ) ) { add_filter( 'generate_post_date', '__return_false' ); add_filter( 'generate_post_author', '__return_false' ); add_filter( 'generate_show_categories', '__return_false' ); add_filter( 'generate_show_tags', '__return_false' ); add_filter( 'generate_show_comments', '__return_false' ); } } );
November 11, 2018 at 9:53 am #724624CarsonSorry for the delay.
Unfortunately, the snippet still doesn’t work. Is this on my side?
Thanks!
November 11, 2018 at 6:41 pm #724828TomLead DeveloperLead DeveloperHmm, it certainly should work: https://codex.wordpress.org/Function_Reference/has_category
You’ve updated the category IDs in the code?
November 12, 2018 at 2:36 pm #725681CarsonYes, I did update the ID’s. If it works for you I really have no idea what I might be doing wrong.
November 12, 2018 at 5:56 pm #725765TomLead DeveloperLead DeveloperI just made an adjustment – can you give it a shot?: https://generatepress.com/forums/topic/disable-post-meta-based-on-category/#post-721411
November 16, 2018 at 5:39 pm #729432CarsonYes, this works!
Once again thanks for the awesome support you guys provide!
-
AuthorPosts
- You must be logged in to reply to this topic.