- This topic has 4 replies, 3 voices, and was last updated 3 years, 6 months ago by
José Antonio.
-
AuthorPosts
-
February 11, 2022 at 12:18 pm #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!
February 11, 2022 at 12:30 pm #2114894Ying
StaffCustomer SupportHi Jose,
I don’t quite understand your question, what exactly are you trying to achieve?
February 17, 2022 at 2:34 am #2122358José 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:
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.
February 17, 2022 at 4:15 am #2122468David
StaffCustomer SupportHi 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);
February 17, 2022 at 2:16 pm #2123332José Antonio
Wow!, works perfect!, thanks Ying and David!
GeneratePress, the best WordPress theme and the best support (and help…)
🙂
-
AuthorPosts
- You must be logged in to reply to this topic.