• 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]

Viewing 15 replies - 16 through 30 (of 43 total)
  • Thread Starter sacconi

    (@sacconi)

    yes, I didnt want to have just one “get_post_meta”, I’ll have one for the category terms and one for the taxonomy terms, but I cant understand your “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.”

    So I dont know what I should add to

    get_post_meta( $post->ID, 'description_'.$lang, true );

    to make it visible

    Moderator bcworkz

    (@bcworkz)

    Concatenate the return value from get_post_meta() to whatever else you want the shortcode to display. Concatenate to the variable your code eventually returns in the end.

    Thread Starter sacconi

    (@sacconi)

    I think the following code cant work

    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, 'category_'.$lang, true) . get_term_meta( $category->term_id, 'tipologia_'.$lang, true) .' '. $content;
    
     }

    Because I dont have a field “tipologia_de”, neither a “category_de”. I tryed to change my field for category in german into “category_de” but I should have done a lot of faults because I could even see the field, I worked on

    add_action( 'category_edit_form_fields', "category_add_term_field");
    
    function category_add_term_field( $term ){
       $category = get_term_meta( $term->term_id, 'function_category', true );
        echo '<tr class="form-field">
          <th scope="row"><label for="function_category">Nome tedesco</label></th>
          <td><input type="text" name="function_category" value="'.$category.'"  />
          <p class="description">Nome del termine in tedesco</p></td>
        </tr>';
    }
    
    add_action( 'saved_category', 'update_category');
    function update_category ( $term_id ) {
      update_term_meta( $term_id, 'function_category', sanitize_text_field ( $_POST['function_category'] ) );
    }
    

    Everywhere I see “category” I replace by “category_de”, this should be simple but…

    Thread Starter sacconi

    (@sacconi)

    Or also in the shortcode should I use archive_de? So that I would syncronize the search box terms with the archive title filter: https://pastebin.com/Xid3M1Ua

    This filter in particular is for the category

    Moderator bcworkz

    (@bcworkz)

    Looks like you’re currently saving under the key “function_category”, but when you add other languages everything will work much better with “_de” appended ( or whatever the current language is aside from the default Italian, “_en”, “_fr”, etc.

    In order to change over to “category_de”, tipologia_de”, “category_en”, etc., first modify the input and saving code. Change all instances of “function_category” to “category_de”. Similar for the Tipologia taxonomy terms. If “archive_de” works better for you, then use that instead. I’ve not been able to keep track of what meta keys you use for what so I cannot judge which is better. What matters is having a language suffix and being consistent in whatever names you decide to use.

    To update all the meta data you’ve already input, use the Better Search and Replace plugin. Search the meta tables for all instances of “function_category” and replace with “category_de” or whatever you decide to use.

    To get the shortcode working after the above changes, I think all you need to do is change $content to $output.

    Thread Starter sacconi

    (@sacconi)

    Ok, I’ve been able to pass from “function_category” to “category_de”, I wonder why my modified shortcode doesnt work about category terms:

    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, 'category_'.$lang, true) .' '. $content;
    
     }
    

    I cant get the “get_term_meta” printed

    Moderator bcworkz

    (@bcworkz)

    Remove this part of the return line: $output .=. It serves no purpose and causes an undefined variable $output error.

    Thread Starter sacconi

    (@sacconi)

    I eliminated it but get_term_meta is still invisible 🙁

    Moderator bcworkz

    (@bcworkz)

    $category is undefined. Try adding:
    $category = wp_get_post_category( $post->ID )[0];

    Thread Starter sacconi

    (@sacconi)

    This breaks the site

    add_shortcode('description', 'translate_desc');
    function translate_desc() {
      global $post;
    $lang = substr( get_locale(), 0, 2);
    
    $category = wp_get_post_category( $post->ID )[0];
    
    $content = get_post_meta( $post->ID, 'description_'.$lang, true );     
    
    return   $output .= get_term_meta( $category->term_id, 'category_'.$lang, true) .' '. $content;
    
     }

    but also

    add_shortcode('description', 'translate_desc');
    function translate_desc() {
      global $post;
    $lang = substr( get_locale(), 0, 2);
    
    $category = wp_get_post_category( $post->ID )[0];
    
    $content = get_post_meta( $post->ID, 'description_'.$lang, true );     
    
    return   $output .= $category .' '. $content;
    
     }
    Moderator bcworkz

    (@bcworkz)

    Remove this part of the return line: $output .=. It serves no purpose and causes an “undefined variable $output” error.

    Thread Starter sacconi

    (@sacconi)

    I did but the layout is broken. I tryed both the above codes without $output .=

    Moderator bcworkz

    (@bcworkz)

    There’s no wp_get_post_categpory() function, it’s wp_get_post_categories().

    Getting the zeroth element with [0] (assigned to $category) gets us a WP_Term object. You cannot return an object from a shortcode handler, you have to extract out a string property. Like this:
    return $category->name .' '. $content;

    Thread Starter sacconi

    (@sacconi)

    If I do

    add_shortcode('description', 'translate_desc');
    function translate_desc() {
      global $post;
    $lang = substr( get_locale(), 0, 2);
    
    $category = wp_get_post_categories( $post->ID )[0];
    
    $content = get_post_meta( $post->ID, 'description_'.$lang, true );     
    
    return  $category.' '. $content; 
    
     }
    

    “$category” returns a number, if i replace “$category” by “$category->name” I get nothing, neither deleting the [0]

    Moderator bcworkz

    (@bcworkz)

    I see, you’re just getting a category ID. The doc page is a little misleading about what is returned. The doc page also says:

    The results from wp_get_post_categories() aren’t cached which will result in a database call being made every time this function is called. Use this function with care. For performance, functions like get_the_category() should be used to return categories attached to a post.

    That’s good advice, if you use get_the_category() instead you’ll get an array of term objects like I was expecting. Despite “get_the_category” using the singular form of category, it does indeed return an array multiple categories if they exist. So you do still need the [0] part afterwards.

Viewing 15 replies - 16 through 30 (of 43 total)

The topic ‘Extending a function’ is closed to new replies.