[Resolved] Disable post meta based on category

Home Forums Support [Resolved] Disable post meta based on category

Home Forums Support Disable post meta based on category

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #717223
    Carson

    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?

    #717640
    Tom
    Lead Developer
    Lead Developer

    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.

    #717878
    Carson

    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?

    #718108
    Tom
    Lead Developer
    Lead Developer

    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;
    } );
    #718646
    Carson

    I tried this on several installs and it doesn’t seem to work. Is this an issue on my side?

    #718773
    Tom
    Lead Developer
    Lead Developer

    Can you link me to a page that has a post with one of those categories?

    #720523
    Carson

    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.

    #720862
    Tom
    Lead Developer
    Lead Developer

    Are you wanting those elements to be removed on the actual category pages, or within the single posts if they belong to a category?

    #721072
    Carson

    within the single posts if they belong to a category

    This is what I need. Sorry if I haven’t been clear before.

    #721411
    Tom
    Lead Developer
    Lead Developer

    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' );
        }
    } );
    #724624
    Carson

    Sorry for the delay.

    Unfortunately, the snippet still doesn’t work. Is this on my side?

    Thanks!

    #724828
    Tom
    Lead Developer
    Lead Developer

    Hmm, it certainly should work: https://codex.wordpress.org/Function_Reference/has_category

    You’ve updated the category IDs in the code?

    #725681
    Carson

    Yes, I did update the ID’s. If it works for you I really have no idea what I might be doing wrong.

    #725765
    Tom
    Lead Developer
    Lead Developer
    #729432
    Carson

    Yes, this works!

    Once again thanks for the awesome support you guys provide!

Viewing 15 posts - 1 through 15 (of 16 total)
  • You must be logged in to reply to this topic.