【问题标题】:Category List is not displaying in a Custom post type类别列表未显示在自定义帖子类型中
【发布时间】:2022-11-04 02:15:51
【问题描述】:

我正在为名为“项目”的自定义帖子类型创建一个模板,并尝试显示分配给单个帖子的所有类别的列表。

<div class="blog-information left full-width">
                    <div class="wrapper">
                    <div class="project-content">
                        <div class="eck-projects-single-start">
                            <a href="/project" style="text-align: left;" class="eck-projects-back-link">< Back to Projects</a>
                            <h2 class="project-title"><?php echo get_the_title(); ?></h2>
                            <p class="eck-projects-single-subtitle"><?php echo $fields['subtitle']; ?></p>
                            <div class="eck-projects-single-categories">
                                <?php echo get_the_category_list( ' \ ' ); ?>
                            </div>
                        </div>

                        <div class="eck-projects-single-content">
                            <?php echo get_the_content(); ?>
                        </div>
                    </div>

                </div>

单个帖子上显示的只是一个空的 div; get_the_category_list 函数没有输入任何类别。

我尝试更改此行: &lt;?php echo get_the_category_list( ' \ ' ); ?&gt; 对此:

        $args = array(
                    'taxonomy' => 'ecprojects',
                    'orderby' => 'name',
                    'order'   => 'ASC'
                );

        $cats = get_categories($args);

        foreach($cats as $cat) {
        ?>
            <a href="<?php echo get_category_link( $cat->term_id ) ?>">
                <?php echo $cat->name; ?>
            </a>
        <?php
        }
        ?>

(ecprojects 是自定义帖子类型的名称),但得到了相同的结果。

【问题讨论】:

  • 如果您只使用echo get_the_category_list();,即没有定义分隔符,它是否有效?
  • 不,事实上,我本来就有这个。结果完全一样。

标签: php wordpress custom-post-type


【解决方案1】:

尝试使用&lt;?php the_category(' ') ?&gt; 而不是&lt;?php echo get_the_category_list( ' ' ); ?&gt;(没有echo...)

【讨论】:

  • 似乎没有什么不同。它仍然空着回来。
  • 其余的东西(内容,标题)是否正确回显?
  • 是的,其他一切都正常进行。
  • “`”通常用于转义字符,所以我在这里也尝试使用默认的the_category();(同样,没有echo
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-17
  • 1970-01-01
  • 2014-10-03
  • 1970-01-01
相关资源
最近更新 更多