Get category name with these post by custom post t

2019-09-24 15:32发布

问题:

I want category name with these post name by custom post type.
Example:

Category Name: Birds
Post Name: Peacock

Category Name: Cities
Post Name: Jaipur

回答1:

Solved.

The answer is: polling-cat is name of taxanomy name of custom post type.

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


标签: php wordpress