Support » Plugin: Advanced Custom Fields » Show taxonomy fields on single posts

  • Resolved schoefl

    (@zemekis148)


    With the help of https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/ I added some fields to the WP taxonomy ‘post_tag’. With a shortcode according to the following pattern, I display the fields in the sidebar.

    function short_func()  {
     $term = get_queried_object();
    ...
     if( get_field('autor') ) {
      $output .= "<h2>".get_field('autor', $term)."</h2>";
      }
    ...
     return $output;
     }
    add_shortcode('autor', 'short_func' );

    This works wonderfully on the archive page (tag.php), but not on the single posts (single.php). I read that I had to add something like $post_id or $term_id to the selector. But I am not competent enough to turn the theoretical description into a working script line.

    Please, how can I also display the values of the tag fields on the single posts with the corresponding keywords?

Viewing 1 replies (of 1 total)
  • Thread Starter schoefl

    (@zemekis148)

    The problem is solved. I found a YouTube video where I was able to understand step by step what is exactly part of the code on https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/ and what is just a placeholder that I have to replace with individual values.

    For everyone with similar problems:

    
    // Shortcode for sidebar
    function short_func() {
     $output = '';
     if( is_single() ) {               // single posts
      $post_tags = get_the_tags();
      $term_id = $post_tags[0]->term_id;
      }
     else {                            // tag archive
      $queried_object = get_queried_object(); 
      $taxonomy = $queried_object->post_tag;
      $term_id = $queried_object->term_id;  
      }
     if( get_field('autor', 'post_tag_'.$term_id) ) {
      $output .= "<h2 class='widget-title'>".get_field('autor', 'post_tag_'.$term_id)."</h2>";
      }
     return $output;
     }
    add_shortcode('shortcode-name', 'short_func' );
    
Viewing 1 replies (of 1 total)
  • The topic ‘Show taxonomy fields on single posts’ is closed to new replies.