根据CPT类别显示帖子

时间:2020-03-18 06:28:55

标签: wordpress custom-post-type

我的帖子类型名为“服务”。它有两个类别“左”和“右”,我希望显示所有“左”类别的帖子。这是我的代码

<?php
    $args = array (
    'post_type'              => 'service',
    'post_status'            => 'publish',
    'order'                  => 'ASC',
    'category_name' => 'Left',
    'posts_per_page'=>-1
    );
    $posts = new WP_Query( $args );
    if ( $posts->have_posts() ) {

    while ( $posts->have_posts() ) {
    $posts->the_post();
    //$image11 = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );

?>

    <li><a href="<?php the_permalink(); ?>"><i class="fa fa-file-text"></i> <?php the_title(); ?></a></li>

 <?php  } } wp_reset_postdata(); ?>

上面的代码不返回任何内容。

2 个答案:

答案 0 :(得分:0)

请尝试以下操作:

$ args 数组上,请使用在“类别”列中插入 category_ID 类别ID。

    <Text><p>I have <strong>5</strong> Year <em>experience</em> in&nbsp; <span style='text-decoration: underline;\'>Flowers</span></p></Text>

答案 1 :(得分:0)

尝试使用类别名称

$args = array(
    'post_type' => 'post',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'category', //name of texonomy
            'field' => 'slug',
            'terms' => 'Left'
        )
    )
);
$my_query = new WP_Query( $args );

if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post();
        //your code
    endwhile;
endif;