Archived topic
Disable post meta based on category
15 replies · Started by Carson on November 2, 2018
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?
Hi 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.
Thanks 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?
For 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;
} );
I tried this on several installs and it doesn't seem to work. Is this an issue on my side?
Can you link me to a page that has a post with one of those categories?
Unfortunately 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.
Are you wanting those elements to be removed on the actual category pages, or within the single posts if they belong to a category?
within the single posts if they belong to a category
This is what I need. Sorry if I haven't been clear before.
Try 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' );
}
} );
Sorry for the delay.
Unfortunately, the snippet still doesn't work. Is this on my side?
Thanks!
Hmm, it certainly should work: https://codex.wordpress.org/Function_Reference/has_category
You've updated the category IDs in the code?
Yes, I did update the ID's. If it works for you I really have no idea what I might be doing wrong.
I just made an adjustment - can you give it a shot?: https://generatepress.com/forums/topic/disable-post-meta-based-on-category/#post-721411
Yes, this works!
Once again thanks for the awesome support you guys provide!