skip to Main Content

I´m trying to display a custom taxonomy image in the frontend on a single post.

Following situation:

  1. Created a CPT for "Weine"
  2. Added a taxonomy "winzer"
  3. Added the following code to my functions.php to display the "winemaker" taxonomy with its description in the single post using a shortcode:
function wpb_catlist_desc() { 
$string = '<div>';
$catlist = get_terms( 'winzer' );
if ( ! empty( $catlist ) ) {
  foreach ( $catlist as $key => $item ) {
    $string .= '<div>'. $item->name . '<br />';
    $string .= '<em>'. $item->description . '</em></div>';
  }
}
$string .= '</div>';
 
return $string; 
}
add_shortcode('wpb_categories', 'wpb_catlist_desc');
  1. Added an Image fieled to the "winzer" taxonomy using ACF. –> Here´s where I´m stuck now.

I need to add another line to the php snippet to also display the image using the shortcode.

Any hints how to get this done? 🙂

Cheers!!!!

2

Answers


  1. Chosen as BEST ANSWER

    Here's how I got it solved:

    function wpb_catlist_desc() { 
    global $post;   
    $string = '<div>';
    $catlist = wp_get_post_terms($post->ID, 'winzer');
    
    
    if ( ! empty( $catlist ) ) {
            foreach ($catlist as $item ) {
                $image = get_field('winzer_bild', 'winzer_'.$item->term_id);
                if(!empty($image)) {
                    $imgurl = $image['url'];
                }
                $string .= '<div>';
                $string .= '<img class="winzer-bild-klein" src="' . $imgurl . '" alt="' . $item->name . '"style="width:100%;"/>';
                $string .= '<h3 class="winzer-name-n">'. $item->name . '</h3>';
                $string .= '<p>'. $item->description . '</p></div>';
    
            }
        }
        $string .= '</div>';
     
        return $string; 
    }
    add_shortcode('wpb_categories', 'wpb_catlist_desc');
    

  2.  function wpb_catlist_desc() { 
        $string = '<div>';
        $catlist = get_terms( 'winzer' );
        if ( ! empty( $catlist ) ) {
            foreach ( $catlist as $key => $item ) {
                $image = get_field('Your_image_field_ID');
                if(!empty($image)) {
                    $imgurl = $image['url'];
                }
    
                $string .= '<div>';
                $string .= '<img src="' . $imgurl . '" alt="' . $item->name . '"/><br />';
                $string .= $item->name . '<br />';
                $string .= '<em>'. $item->description . '</em></div>';
            }
        }
        $string .= '</div>';
     
        return $string; 
    }
    add_shortcode('wpb_categories', 'wpb_catlist_desc');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search