ACF - Display image from Custom Post Type custom C

2019-07-25 04:02发布

I have a problem and I believe the answer lies here: https://www.advancedcustomfields.com/resources/get-values-from-a-taxonomy-term/ But I cant figure out how to apply it to my code...

I have used Advanced Custom Fields to add an image field inside the categories of a custom post type. My custom post type is call Short Courses, and the category name is Course Types.

Here's the loop:

        <?php
            $customPostTaxonomies = get_object_taxonomies('short_courses');

            if(count($customPostTaxonomies) > 0)
            {
                 foreach($customPostTaxonomies as $tax)
                 {
                     $args = array(
                          'orderby' => 'name',
                          'show_count' => 0,
                          'pad_counts' => 0,
                          'hierarchical' => 1,
                          'taxonomy' => $tax,
                          'title_li' => '',
                          'hide_empty' => FALSE
                          );

                        $categories = get_categories( $args );
                        foreach ( $categories as $category ) {
                            echo '

                            <div class="one-half sc-cat-items">
                                <img src="' . get_field('course_type_image', $category->name) . '">

                                <h2>
                                    <a href="' . get_category_link( $category->term_id ) . '">' . $category->name . '</a>
                                </h2>
                            </div>';
                        }

                 }
            }
        ?>

I've tried to add this line in to retrieve the image for each category:

<img src="' . get_field('course_type_image', $category->name) . '">

This prints out the <img src=""> tag, but it isn't filling in the URL for some reason...

Ive also tried: <img src="' . get_field('course_type_image') . '"> on it's own, but the same result

Currently it looks like this:

enter image description here

and I'm trying to display the image for each category so it looks like this:

enter image description here

1条回答
我想做一个坏孩纸
2楼-- · 2019-07-25 04:23

Try Below code :

 $taxonomy = $category->taxonomy;
 $term_id = $category->term_id; 
 $slug =  $taxonomy . '_' . $term_id; 

 $img = get_field('course_type_image',$slug);

if(sizeof($img))
{
      echo '<img src="'.$img['url'].'">';
}

You can modify the loop with my code as below :

foreach ( $categories as $category ) {
      echo '<div class="one-half sc-cat-items">';
              $taxonomy = $category->taxonomy;
              $term_id = $category->term_id; 
              $slug =  $taxonomy . '_' . $term_id; 

              $img = get_field('course_type_image',$slug);
              if(sizeof($img))
              {
                   echo '<img src="'.$img['url'].'">';
              }    

         echo '<h2><a href="' . get_category_link( $category->term_id ) . '">' . $category->name . '</a></h2>';
      echo '</div>';
}
查看更多
登录 后发表回答