Extending a function
-
How can I insert this:
<strong> <?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?> </strong>to this:
add_shortcode('incipit', 'translate_incipit'); function translate_incipit() { global $post; $german = get_post_meta( $post->ID, 'incipit_de', true ); if ( 'de_DE' == get_locale() && ! empty( $german )){ return $german; } else { return get_post_meta( $post->ID, 'incipit_it', true ); } }what I want to do is having the category name printed before the value of “get_post_meta” , having in mind that I should print the name of the category eventually translated
The page I need help with: [log in to see the link]
-
The code you want to insert needs to be modified for use in a shortcode callback because shortcode callbacks must not ever echo out content. Instead, all output should be collected into a single variable. Before starting a foreach loop, initialize a variable by assigning an empty string or the first part of the output. For example:
$output = '';or$output = '<strong>';Anywhere content is output, such as with
echo, instead concatenate the output to $output. For example:
$output .= $category->cat_name . ' ';
But if the category name needs to maybe be translated it will be more complicated than this. Addressed below. Also, are you sure aboutcat_name? That’s an unusual object property if we’re using WP_Term objects. I think you want$category->name.After the foreach loop completes, don’t forget the closing strong tag:
$output .= '</strong>';Where all of this goes into your current shortcode callback depends on where you want output to occur in relation to post meta content. Before or after? Then add the category code before or after the post meta code.
Modify the post meta code. Instead of returning content, concatenate post meta content to $content. In the end after all is said and done, you will finally do:
return $content;Now, how to work in possible translations of category names. The first thing your function should do is get the locale’s 2 character language code so you can use it to get the right translation. Let’s say the language code is assigned to
$lang. Then you could get the right language’s post meta with something like:
$output .= get_post_meta( $post->ID, 'incipit_'.$lang, true );You’d do something similar for category names, except there is only term meta for non-Italian languages. If the locale language is “it”, $category->name is fine, but otherwise it has to come from term meta. Thus you need to determine the right value to output before concatenating it to $output. Instead of just
$output .= $category->name . ' ';
do something like:
$output .= 'it' == $lang ? $category->name : get_term_meta( category->term_id, 'tipologia_'.$lang, true) .' ';In case you’re not familiar with the
$condition ? 'its true':'its false';construct, it’s a kind of shorthand for typical if/then/else statements. It’s called a “ternary” statement. It’s equivalent to:if ( $condition ) { 'its true'; } else { 'its false'; }Will this become the code I was asking for in your other topic about filtering a taxonomy for German? Then I guess I’m answering my own question 🙂 No filtering is necessary with this approach.
Do note that this requires you to alter the meta key names to be similar to tipologia_de, tipologia_en, tipologia_fr, etc. This means updating the code that saves term custom field data related to translated term names.
First of all I made a mistake about the shortcode I needed to use. That said, it makes sense a function like this?
add_shortcode('description', 'translate_desc'); function translate_desc() { global $post; $content= get_post_meta( $post->ID, 'description_it', true ); $german = get_post_meta( $post->ID, 'description_de', true ); if ( 'de_DE' == get_locale() && ! empty( $german )){ $output .= $category->name . ' '. $german ; } else { $output .= $category->name . ' '. $content ; } }I have just transformed the post meta content into a variable, or better 2 variables, according to the language, than I keep the conditional structure and change the output according to the language
I see now that I havent solutioned the category term name in german
Whether the meta data exists or not does not invalidate the shortcode. You’re approach is fine if it works for you. It’s more cumbersome than what I was suggesting, but whatever works is good. However, you’d need to modify the code anytime you add another language. I was trying to avoid that by dynamically constructing the meta key based on the current locale’s language. Thus the shortcode code wouldn’t need to change when another language is added.
But you’d still need additional meta box fields in the term editor screen, as well as the save code for each language. Right now you apparently have none of this in place. It’s the same concept as you’ve already done for custom taxonomy terms. Just be sure you’re thorough in adjusting copies of what you’ve already done to work for categories as well. For example, when you rename the function name, be sure the name in the add_filter() call is also changed to match. Be sure every instance of the custom taxonomy name is changed to “category”.
Since I desire more languages I’ll try to undestand better your code
I think my own code has some errors because the shortcode outputs nothing. I can’t see where is the problem. I checked the code with https://phpcodechecker.com/
I tryed to write a new code according to your suggestions, it makes a sense?
add_shortcode('description', 'translate_desc'); function translate_desc() { global $post; $content= get_post_meta( $post->ID, 'description_it', true ); $german = get_post_meta( $post->ID, 'description_de', true ); if ( 'de_DE' == get_locale() && ! empty( $german )){ $output .= 'de' == $lang ? $category->name : get_term_meta( category->term_id, 'tipologia_'.$lang, true) .' '. $german ; } else { $output .= 'it' == $lang ? $category->name : get_term_meta( category->term_id, 'tipologia_'.$lang, true) .' '. $content; } }About the above code, with php checker “No issues detected.” but it broke the site
I’ve not checked your code thoroughly, but the glaring problem is you forgot
return $output;at the end.Additionally, you need to extract the 2 char language code from the 5 char locale value (with substr()) and assign it to
$lang. Then you can get post meta of whatever language (assuming the meta keys are all similarly named) with just this alone:
$content = get_post_meta( $post->ID, 'description_'.$lang, true );Then you can generate output in whatever language with just this:
$output .= get_term_meta( $category->term_id, 'tipologia_'.$lang, true) .' '. $content;
No language conditionals needed, everything depends on the value of $lang. But you’ll still want to check for empty values and set $output to some default value instead of empty.A couple more issues though. You should initialize $content to an empty string before trying to concatenate with
.=. And currently $category is undefined so you cannot properly do $category->term_id. I’m not sure where that would come from though. Maybe wp_get_post_categories()?I tryed with the following
//prova shortcode descrizione tedesco e tutte le lingue add_shortcode('description', 'translate_desc'); function translate_desc() { global $post; $content = get_post_meta( $post->ID, 'description_'.$lang, true ); return $output .= get_term_meta( $category->term_id, 'tipologia_'.$lang, true) .' '. $content; }I think something is missing, maybe $lang should be defined somewhere? This code has no output in my test for real
Yes PHP despises undefined variables 🙂 You need to assign the current 2 char language code to $lang. You can extract it from the locale with substr().
$lang = substr( get_locale(), 0, 2);Ok, I did the following:
add_shortcode('description', 'translate_desc'); function translate_desc() { global $post; $lang = substr( get_locale(), 0, 2); $content = get_post_meta( $post->ID, 'description_'.$lang, true ); return $output .= get_term_meta( $category->term_id, 'tipologia_'.$lang, true) .' '. $content; }But I get in return only $content, the get_term_meta doesnt appear
Looks like
$categoryis also undefined. The term we’re after isn’t actually a category, it’s tipologia, right? Maybe the variable name ought to be$tipologiato prevent confusion?Whatever the name, you need to get the term assigned to the current post. You can do that with wp_get_post_terms(). Note that an array of term objects are returned, even if there is only one term assigned to the post. So you’d need to extract the first element (index
[0]) out of the array before using its term_id property.I’d like to have both the category and the taxonomy (tipologia) in the function, so I will concatenate 2 get_term_meta. After having visited the link you wrote, it seems to me I could do the following:
get_term_meta( int $post_id, string[0], $category->term_id, 'tipologia_'.$lang, true)You cannot add extra args to function calls. get_term meta() takes 3 args and that’s it. To display meta data for two terms you need to make two separate calls. You may then concatenate the returned values together.
Be sure all variables that you pass to a function have proper values assigned.
The topic ‘Extending a function’ is closed to new replies.