【发布时间】: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 函数没有输入任何类别。
我尝试更改此行:
<?php echo get_the_category_list( ' \ ' ); ?>
对此:
$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