首页 > 解决方案 > 遍历 wp_get_post_terms 并回显分类名称

问题描述

我有一个名为“项目”的自定义帖子类型和 8 个 CPT 类别的列表。在我网站的主页上,我想调用最新的帖子并回显项目名称、摘录和类别。我有名称和摘录,但我不知道如何引入自定义分类法。

我知道我可以使用 wp_get_post_terms() 来获取分配给特定帖子的分类术语,但这会返回一个术语对象数组。我不知道如何循环返回并每次回显 $term->name。

<?php
$args = array(
    'meta_key'     => 'featured',
    'meta_value'   => '1',
    'post_type' => 'project'
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
    $query->the_post();
    $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' ); ?>

    <div class="cpt-feature" style="background: url('<?php echo $backgroundImg[0]; ?>') center center no-repeat; background-size: cover;">
    <?php echo get_the_post_thumbnail( $page->ID, 'thumbnail' ); ?>

    <?php
    echo "<div class='cpt-overlay'>";
    echo "<div class='project-information'>";
    // NEED TO INSERT CPT CATEGORY HERE
    echo "<h3>";
        the_title();
    echo "</h3>";
    echo "<p>";
    echo get_field('intro_blurb');
    echo "<p><a href='" . get_permalink() . "'>View Project</a></p>";
    echo "</div>";
    echo "</div>";
    echo "</a>";
}
wp_reset_postdata(); } else { }?>

标签: phpwordpresscustom-post-typecustom-taxonomy

解决方案


wp_get_post_terms()您可以通过一个简单的 foreach 循环非常轻松地遍历返回值:

$terms = wp_get_post_terms( $post->ID, 'your-taxonomy-here' );

foreach( $terms as $term){
    echo $term->name;
}

但是,如果您只需要一个简单的链表,您甚至可能不需要这样做,您可以使用它get_the_term_list()来代替。

另一个注意事项是,您不需要每行有一个回显。在这种情况下,最好避开 PHP 并使用 WP 中可用的自回显函数。并通过处理缩进和格式来帮助未来的你。以下是我的处理方法get_the_term_list()

<?php
    $args = array(
        'meta_key'   => 'featured',
        'meta_value' => '1',
        'post_type'  => 'project'
    );

    $query = new WP_Query( $args );

    if( $query->have_posts() ){
        while( $query->have_posts() ){
            $query->the_post();

            $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); ?>

            <div class="cpt-feature" style="background: url('<?= $backgroundImg[0]; ?>') center center no-repeat; background-size: cover;">
                <?php the_post_thumbnail( $page->ID, 'thumbnail' ); ?>
                <div class="cpt-overlay">
                    <div class="project-information">
                        <?php
                            echo get_the_term_list( $post->ID, 'your-taxonomy-here' );
                            the_title( '<h3>', '</h3>' );
                        ?>
                        <p>
                            <?php the_field( 'intro_blurb' ); ?>
                            <a href="<?php the_permalink(); ?>">View Project</a>
                        </p>
                    </div>
                </div>
            </div>
        <?php }
        wp_reset_postdata();
    }
?>

或者,如果您不想要一个链表,而是想使用wp_get_post_terms()它,只需交换函数并添加一个foreach

<?php
    $args = array(
        'meta_key'   => 'featured',
        'meta_value' => '1',
        'post_type'  => 'project'
    );

    $query = new WP_Query( $args );

    if( $query->have_posts() ){
        while( $query->have_posts() ){
            $query->the_post();

            $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); ?>

            <div class="cpt-feature" style="background: url('<?= $backgroundImg[0]; ?>') center center no-repeat; background-size: cover;">
                <?php the_post_thumbnail( $page->ID, 'thumbnail' ); ?>
                <div class="cpt-overlay">
                    <div class="project-information">
                        <?php
                            foreach( wp_get_post_terms( $post->ID, 'your-taxonomy-here' ) as $term )
                                echo $term->name;

                            the_title( '<h3>', '</h3>' );
                        ?>
                        <p>
                            <?php the_field( 'intro_blurb' ); ?>
                            <a href="<?php the_permalink(); ?>">View Project</a>
                        </p>
                    </div>
                </div>
            </div>
        <?php }
        wp_reset_postdata();
    }
?>

推荐阅读