在 wordpress 的自定义页面模板中显示帖子分类信息

Posted

技术标签:

【中文标题】在 wordpress 的自定义页面模板中显示帖子分类信息【英文标题】:Show post taxonomy information in custom page template in wordpress 【发布时间】:2017-11-25 10:08:41 【问题描述】:

我有一个正在通过子主题编辑的主题。我已经能够创建自定义页面模板,因为我想使用自定义页面添加特定帖子类型的分类信息。

自定义页面有如下代码

<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 -->

我需要在每个描述下方添加分类信息。假设帖子类型的名称是食物我如何在上面添加代码以显示每个帖子的分类(创建、大小、时间、类型)信息。

【问题讨论】:

【参考方案1】:

根据帖子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; ?>

【讨论】:

好的,会尝试并恢复

以上是关于在 wordpress 的自定义页面模板中显示帖子分类信息的主要内容,如果未能解决你的问题,请参考以下文章

WordPress中的自定义博客存档页面

根据带有加载更多按钮的自定义查询获取 WordPress 帖子

无法显示来自 Wordpress REST API 自定义端点的自定义字段

多个永久链接的自定义模板页面wordpress

Wordpress 在自定义帖子类型存档页面上按日期排序

用于显示距当前日期超过 8 个月的 WordPress 自定义帖子类型的自定义查询