Site logo

Archived topic

Use singular and plural text with "generate_inside_post_meta_item_output"

4 replies · Started by José Antonio on February 11, 2022

Viewing posts 1–5 of 5

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!

Hi Jose,

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

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.

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);

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

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

:)

This archived topic is closed to new replies.