Changing category label in post meta


#1

Ok, so I'm still trying to get my head around the Framework concept. I know I'm not supposed to make changes to core files and only change things using hooks and functions as much as possible.

If I want to change the word "Category" in the post meta to "Posted in:" how would you recommend I do that in the child theme?


#2

Hi phpadam,
Text wrapped in translated strings can be modified in the following way. Just make sure all the conditions are met.

function translate_meta( $translation, $text, $domain ) { $translations = &get_translations_for_domain( $domain ); if ( $text == 'Category' && !is_admin() ) { return $translations->translate( 'Posted in' ); } return $translation; }

add_filter( ‘gettext’, ‘translate_meta’, 10, 4 );


#3

That’s an interesting way to do it, but what if I wanted to replace the word “Category” with an image or add an image to that line?

Would the translation support the img tag?


#4

And the answer is yes!


#5

I’m not sure I really have an understanding of how that translation function is working. If I wanted to do the same thing for the Archive Page Titles (which seem to be created at the top of loop-common.php) and change “Category: Category Name” to “Posted in: Category name”, how would I create another function. Maybe that will help me understand.


#6

The gettext filter is used to translate strings in WordPress. So any string wrapped in __( ), _e(), _x() …etc could be translated.

Now if you have a look at the text you want to change here, it is wrapped in this way -> __( ‘Category: %s’, ‘rtPanel’ )
%s here being variable. So the code you are looking for would be.

function translate_meta( $translation, $text, $domain ) { $translations = &get_translations_for_domain( $domain ); if ( $text == 'Category: %s' && !is_admin() ) { return $translations->translate( 'Posted in %s' ); } return $translation; }

#7

Thanks for the clarity Joshua!

So ANYPLACE in the site (not just in the post meta) where the text created by ‘Category: %s’ appears would be translated to ‘Posted in %s’ (as long as its wrapped in __( ), _e(), _x() …etc)?

This works fine in this case, but I’m not sure it’s a great overall solution because the alteration might only be required on a certain page or in a certain location, and not in EVERY place the word or phrase is generated (e.g. I don’t think it can’t be restricted to a TOP post meta section ONLY.)

Is the ‘!is_admin’ restriction included just so it only appears to non-admin users?

Since you are using a conditional to restrict to non-admins, I assumed you could add other conditionals to restrict the altered translation to certain pages or post categories. So I modified the function to include the translations for BOTH the Post Meta and the Category Title (but ONLY if the category title was on a certain Category display). And this works great:

// Change some translations

function translate_meta( $translation, $text, $domain ) {
$translations = &get_translations_for_domain( $domain );

// change 1 - Changes Category to Posted in - for post meta
if ( $text == ‘Category’ && !is_admin() ) {
return $translations->translate( ‘Posted in’ );
}
//end change 1

// change 2 - Changes Category to Whoo Hoo! for Category Titles ONLY if category is my-category
if ( is_category(‘my-category’) && $text == ‘Category: %s’ && !is_admin() ) {
return $translations->translate( ‘Whoo Hoo! %s’ );
}
//end change 2

return $translation;
}

add_filter( ‘gettext’, ‘translate_meta’, 10, 4 );

I’m posting it for your opinion and to flesh out the thread. Thanks again for all your help.


#8

The !is_admin() was just to make sure similar text on the admin panel does not get replaced ( just a minor precaution ). You can play around with the translations by supplying different conditions. But I don’t think any of the strings wrapped in the translation functions have the same text. So it shouldn’t be much of an issue in rtPanel atleast.


#9