显示按分类法分组的自定义帖子类型的摘录,缩略图和标题?

时间:2018-04-21 21:37:59

标签: php wordpress

我想展示,例如具有自定义帖子类型和特定分类的所有帖子的标题。

我试过以下:

global $post;

$myposts = get_posts(array(
    'post_type' => 'my-post-type',
    'tax_query' => array(
        array(
            'taxonomy' => 'post-type-category',
            'terms' => 'new-posts')
            )
    )
);

foreach ($myposts as $mypost) {
    echo $mypost->post_title;
}

1 个答案:

答案 0 :(得分:2)

您可以使用WP Query

<?php
$args = array(
    'post_type' => 'my-post-type',
    'tax_query' => array(
        array(
            'taxonomy' => 'post-type-category',
            'field'    => 'slug',
            'terms'    => 'new-posts',
        ),
    ),
);

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        the_title() ;  // title
        the_excerpt(); // excerpt
        the_post_thumbnail(); // post thumbnail
    }
    wp_reset_postdata();
} else {
    // no posts found
}