【问题标题】:Get category name with these post by custom post type通过自定义帖子类型获取这些帖子的类别名称
【发布时间】:2015-05-14 07:47:58
【问题描述】:

我想要通过自定义帖子类型使用这些帖子名称的类别名称。
示例:

Category Name: Birds
Post Name: Peacock

Category Name: Cities
Post Name: Jaipur

【问题讨论】:

  • 好的,那么对于Peacock,类别名称是Birds,对于Jaipur,类别名称是Cities。对于您发布的问题,这是您可以获得的最佳答案。如需更详细的帮助,请发布您所做的。究竟是什么问题并写一个正确的问题,关于您正在使用哪种语言(php、Java、C、C++ 等)。不要只是将 php/wordpress 放在标签上。
  • 忘了告诉,请先尝试 WP 帮助和支持。顺便说一句,你见过这个(假设你正在使用 Wordpress)codex.wordpress.org/Function_Reference/wp_get_post_categories

标签: php wordpress


【解决方案1】:

解决了。

答案是: polling-cat 是自定义帖子类型的分类名称。

$terms = get_terms( 'polling-cat' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
    foreach ( $terms as $term ) {
        echo $term->name;
        $args = array(
            'posts_per_page' => 5,
            'polling-cat' => $term->slug,
            'post_type' => 'polling',
            'post_status' => 'publish',
        );
        $myposts = get_posts( $args );
        foreach ( $myposts as $post ) : setup_postdata( $post );
            the_title();
        endforeach;
        wp_reset_postdata();
    }
}

【讨论】:

    猜你喜欢
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 2016-07-16
    • 2015-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多