ACF-显示来自“自定义帖子类型”自定义类别的图像 [英] ACF - Display image from Custom Post Type custom Category

查看:135
本文介绍了ACF-显示来自“自定义帖子类型”自定义类别的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题,我相信答案就在这里:



,我正在尝试显示每个类别的图像,如下所示:



解决方案

尝试以下代码:

  $ taxonomy = $ category->分类; 
$ term_id = $ category-> term_id;
$ slug = $ taxonomy。 ‘_’。 $ term_id;

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

if(sizeof($ img))
{
echo’< img src =’。$ img [’url’]。’>’;
}

您可以使用以下代码修改循环:

  foreach($ categories as $ category){
echo'< div class =单项sc-cat-items> ;';
$ taxonomy = $ category->分类法;
$ 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>’;
}


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:

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

解决方案

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>';
}

这篇关于ACF-显示来自“自定义帖子类型”自定义类别的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆