Site logo

[Resolved] Use singular and plural text with “generate_inside_post_meta_item_output”

Home Forums Support [Resolved] Use singular and plural text with “generate_inside_post_meta_item_output”

Home Forums Support Use singular and plural text with “generate_inside_post_meta_item_output”

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #2114874
    José Antonio

    Hi, how use singular and plural text with generate_inside_post_meta_item_output function when add the text before publishing the category meta?

    This is my snippet:

    add_filter('generate_inside_post_meta_item_output', function ($output, $item) {
      if ('categories' === $item) {
        return __('Category: ', 'domain.com') . ' ';
      }
      if ('tags' === $item) {
        return __('Topic: ', 'domain.com') . ' ';;
      }
      return $output;
    }, 50, 2);

    Thank you!

    #2114894
    Ying
    Staff
    Customer Support

    Hi Jose,

    I don’t quite understand your question, what exactly are you trying to achieve?

    #2122358
    José Antonio

    Hi Ying,

    First for all, sorry for my poor explanation.

    I found this snippet in the GeneratePress documentation:

    add_filter( 'generate_inside_post_meta_item_output', function( $output, $item ) {
    	if ( 'categories' === $item ) {
            return ' Published in ';
        }
        return $output;
    }, 50, 2 );

    Allows adding custom text before publishing the category or tag meta.

    I have used it for categories and tags on my website and it works perfectly with this snippet:

    add_filter('generate_inside_post_meta_item_output', function ($output, $item) {
      if ('categories' === $item) {
        return __('Categories: ', 'domain.com') . ' ';
      }
      if ('tags' === $item) {
        return __('Topics: ', 'domain.com') . ' ';;
      }
      return $output;
    }, 50, 2);

    Result:

    Custom text before meta

    But I would like to customize it to use plural and singular texts, for example, according to the attached screenshot, for a single tag and category, it should appear “Category” and “Topic”.

    Thanks again.

    #2122468
    David
    Staff
    Customer Support

    Hi there,

    you would do something like this:

    add_filter('generate_inside_post_meta_item_output', function ($output, $item) {
        global $post;
        $cat_terms = wp_get_post_terms( $post->ID, 'category', array( "fields" => "slugs" ) );
        $tag_terms = wp_get_post_terms( $post->ID, 'post_tag', array( "fields" => "slugs" ) );
    
        if ('categories' === $item) {
            return _n('Category: ', 'Categories: ', count( $cat_terms ), 'text-domain');
        }
    
        if ('tags' === $item) {
            return _n('Topic: ', 'Topics: ', count( $tag_terms ), 'text-domain');
        }
    
        return $output;
    
    }, 50, 2);
    #2123332
    José Antonio

    Wow!, works perfect!, thanks Ying and David!

    GeneratePress, the best WordPress theme and the best support (and help…)

    🙂

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