【问题标题】:ACF - Display image from Custom Post Type custom CategoryACF - 显示来自自定义帖子类型自定义类别的图像
【发布时间】:2018-05-16 23:01:14
【问题描述】:

我有一个问题,我相信答案就在这里:https://www.advancedcustomfields.com/resources/get-values-from-a-taxonomy-term/ 但我不知道如何将它应用到我的代码中......

我已使用高级自定义字段在自定义帖子类型的类别中添加图像字段。我的自定义帖子类型是短期课程,类别名称是课程类型。

这是循环:

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

                 }
            }
        ?>

我尝试添加这一行来检索每个类别的图像:

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

这会打印出&lt;img src=""&gt; 标签,但由于某种原因它没有填写 URL...

我自己也试过:&lt;img src="' . get_field('course_type_image') . '"&gt;,但结果相同

目前看起来是这样的:

我正在尝试显示每个类别的图像,所以它看起来像这样:

【问题讨论】:

    标签: php wordpress custom-post-type categories advanced-custom-fields


    【解决方案1】:

    试试下面的代码:

     $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'].'">';
    }
    

    您可以使用我的代码修改循环,如下所示:

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

    【讨论】:

    • 非常感谢 Ankita - 但是我应该在哪里添加此代码?我试图将它添加到图像标签的位置,甚至只是在 echo 语句中,但它不起作用并破坏了代码?
    • 愚蠢的问题,我知道,但我认为我需要以某种方式将它构建到我现有的循环中,但我不知道该怎么做。
    • @Shaun:我已经修改了我的答案!尝试用我的替换你的循环。希望对您有所帮助!
    • @Ankits - 你!现在似乎将图像输出到每种课程类型,但图像源是通过 h.. 所以我会仔细看看 :)
    • 顺便说一下,如果有帮助的话,这里的页面:staging.seedcreativeacademy.co.uk/short-courses - 仍然无法找出问题所在...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多