【问题标题】:Wordpress - Category imageWordpress - 类别图片
【发布时间】:2021-02-27 14:50:23
【问题描述】:

我知道这个问题被问了很多次,但没有一个解决方案对我有用。 我想在网站上显示我的类别及其缩略图。

我尝试使用类别图像插件,但由于某种原因,该插件停止工作,并且没有让我有机会将图片附加到类别(类别设置中未显示添加图像按钮)。

在此之后我创建了自定义字段,我在其中创建了一个分类法等于类别。有了这个,我可以将图像与类别配对,但我不知道如何将其添加到代码中。

这是我的代码:

<section id="categories">
    <div class="center-box">
        <div class="categories">
            <?php 
            $categories = get_categories(array ('parent' => 0));
            foreach($categories as $category) { ?>
            <div class="category-box">
                <a class="category-img" href="<?php echo get_category_link($category->term_id); ?>">
                    <img class="img" src="<?php echo z_taxonomy_image_url($category->term_id); ?>"/>
                    <div class="category-name">
                    <h3> <?php  echo $category->name; ?> </h3>
                    </div>
                </a>
            </div>
        
        <?php
        $args = array(
        'numberposts' => 1,
        'orderby' => 'date', 
        'order' => 'DESC', 
        'category' => $category->term_id, );
        ?>
        
            <?php   } ?>
        </div>
    </div>
</section>

它仍然有以前的类别图像插件代码,不再起作用了。

有人可以帮我如何用图片显示类别吗?

谢谢!

【问题讨论】:

    标签: php wordpress taxonomy


    【解决方案1】:

    如果您还没有,我建议为此使用Advanced Custom Fields 插件。根据您的工作假设,您可以执行以下操作:

    <section id="categories">
    <div class="center-box">
        <div class="categories">
            <?php 
            $categories = get_categories(array ('parent' => 0));
            foreach($categories as $category) { ?>
            <div class="category-box">
                <a class="category-img" href="<?php echo get_category_link($category->term_id); ?>">
                    <img class="img" src="<?php echo wp_get_attachment_image_src( get_field( 'thumbnail', "term_$category->term_id" ), 'small' ) ?>"/>
                    <div class="category-name">
                    <h3> <?php  echo $category->name; ?> </h3>
                    </div>
                </a>
            </div>
        
        <?php
        $args = array(
        'numberposts' => 1,
        'orderby' => 'date', 
        'order' => 'DESC', 
        'category' => $category->term_id, );
        ?>
        
            <?php   } ?>
        </div>
    </div>
    

    【讨论】:

      猜你喜欢
      • 2014-04-27
      • 1970-01-01
      • 1970-01-01
      • 2019-12-19
      • 1970-01-01
      • 2021-11-17
      • 1970-01-01
      • 2018-04-06
      • 1970-01-01
      相关资源
      最近更新 更多