繁体   English   中英

显示带有类别的 Wordpress 类别图像

[英]Displaying Wordpress category images with category

我有这么一段代码:

<div class="wrap sc-cat-container">

        <?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">
                                <h2>
                                    <a href="' . get_category_link( $category->term_id ) . '">' . $category->name . '</a>
                                </h2>

                            </div>';
                        }



                 }
            }
        ?>

    </div>

这将显示我的自定义帖子类型“短期课程”的类别列表 这是目前的外观: http : //staging.seedcreativeacademy.co.uk/short-courses/

这很棒,效果很好,但我也想为我的类别添加图像,它看起来像这样:

在此处输入图片说明

我有高级自定义字段,我在其中创建了一个图像字段,因此我现在可以为每个类别分配一个图像。 到现在为止还挺好!

当我尝试在上面的代码中显示图像时,主要问题出现了......

这是在任何正常情况下显示图像的代码:

<img src="<?php the_field('course_type_image'); ?>">

但是我想我需要将它添加到上面的代码中......所以我想出了这个但是它不起作用,因为我认为我不能将一个 php 标签放在另一个 PHP 标签中!?

<div class="wrap sc-cat-container">

        <?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="<?php the_field('course_type_image'); ?>">
                                <h2>
                                    <a href="' . get_category_link( $category->term_id ) . '">' . $category->name . '</a>
                                </h2>
                            </div>';
                        }

                 }
            }
        ?>

    </div>

所以我有点困惑...

您正在尝试回显一个字符串,但在您的字符串中,您正在调用一个函数,该函数本身回显而不是返回一个字符串:

echo '
<div class="one-half sc-cat-items">
    <img src="<?php the_field('course_type_image'); ?>">
    <h2>
        <a href="' . get_category_link( $category->term_id ) 
                    . '">' . $category->name . '</a>
    </h2>
</div>';

因此,当尝试在要回显的字符串中调用函数the_field() 时,它会再次尝试回显。

您需要使用get_field()函数返回字段的值。

另外,删除 img 源代码行中的 php 标签,因为您已经在一个开放的<?php标签中:

'<img src="' . get_field('course_type_image') . '">'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM