【问题标题】:Wordpress - get category name of custom post typeWordpress - 获取自定义帖子类型的类别名称
【发布时间】:2020-10-23 06:46:20
【问题描述】:

我的代码返回默认帖子类型“post”的类别名称:

<div class="row masonary-wrap">

<?php 
$args = array(
    'post_type' => 'project',
    'post_status' => 'publish',
    'posts_per_page' => '6'
);

$query = new WP_Query( $args );

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

        $categories = get_categories( $args ); 

        foreach ( $categories as $cat ) : ?>

            <div class="col-lg-4 col-md-6 col-12 port-item mb-30 <?php echo esc_attr( $cat->cat_name ); ?>">    
                <div class="project">
                    <div class="proj-img">
                        <div class="proj-overlay">
                            <h5><?php the_title(); ?></h5>                          
                        </div>
                    </div>
                </div>
            </div>

        <?php
        endforeach;
    endwhile;
    wp_reset_postdata();
endif; ?>

</div>

我需要显示自定义帖子类型“项目”的类别名称或更好的 slug,而不是默认帖子!

【问题讨论】:

标签: php wordpress


【解决方案1】:

你可以做这样的事情。请检查以下代码

$terms = get_the_terms( $post->ID , 'project' );
foreach ( $terms as $term ) {
    echo $term->name;
}

谢谢,

【讨论】:

    猜你喜欢
    • 2015-11-09
    • 1970-01-01
    • 2023-04-04
    • 2023-04-05
    • 2015-05-14
    • 2011-06-24
    • 1970-01-01
    • 2021-10-01
    • 1970-01-01
    相关资源
    最近更新 更多