【问题标题】:Loop through wp_get_post_terms and echo out taxonomy name遍历 wp_get_post_terms 并回显分类名称
【发布时间】:2018-06-17 13:47:56
【问题描述】:

我有一个名为“项目”的自定义帖子类型和一个包含 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 { }?>

【问题讨论】:

    标签: php wordpress custom-post-type custom-taxonomy


    【解决方案1】:

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

    $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();
        }
    ?>
    

    【讨论】:

    • 非常感谢您的详细回复!这是一个很好的开始,我设法让它发挥作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多