在wordpress中的自定义页面模板中显示分类信息
问题描述:
我有一个主题,我通过儿童主题进行编辑。我已经能够创建自定义页面模板,因为我想使用自定义页面添加特定帖子类型的分类信息。在wordpress中的自定义页面模板中显示分类信息
自定义页面具有下面的代码
<div id="primary" class="content-area col-sm-12 col-md-8 <?php echo of_get_option('site_layout'); ?>">
<main id="main" class="site-main" role="main">
<?php while (have_posts()) : the_post(); ?>
<?php get_template_part('content', 'page'); ?>
<?php
// If comments are open or we have at least one comment, load up the comment template
if (comments_open() || '0' != get_comments_number()) :
comments_template();
endif;
?>
<?php endwhile; // end of the loop. ?>
</main><!-- #main -->
</div><!-- #primary -->
我需要添加以下每条说明的分类信息。假设帖子类型的名称是食品,我如何向上面添加代码以显示每个帖子的分类(创建,大小,时间,类型)信息。
答
根据帖子ID在类别列表的while循环中添加以下代码。
//Returns Array of Term for "my_taxonomy"
$terms = get_the_terms(get_the_ID(), 'my_taxonomy');
if ($terms && ! is_wp_error($terms)) :
$terms_links = array();
foreach ($terms as $term) {
$terms_links[] = $term->name;
}
$on_terms = join(", ", $terms_links);
?>
<p>
<?php printf(esc_html__('Categories : <span>%s</span>', 'textdomain'), esc_html($on_terms)); ?>
</p>
<?php endif; ?>
好的会尝试并恢复 –