如何在wordpress中显示自定义帖子类别名称列表

时间:2016-04-16 05:57:44

标签: php wordpress custom-post-type advanced-custom-fields

我的wordpress网站有两种自定义帖子类型(图库和活动)。我想在图库帖子类型中显示类别。但我的代码显示了两种帖子类型(图库和活动)中的所有类别。我正在使用高级自定义字段(ACF)和CPT-UI来创建自定义帖子。如何仅在图库中显示类别。这是我的代码。

<?php $catargs = array(
 'post_type'              => 'gallery',

 'orderby'                  => 'name',

 'order'                    => 'ASC',

 ); 

 $categories = get_categories( $catargs );

 foreach ($categories as $category) {?>
<h1 class="entry-title" style="padding-top:30px;">
<?php     echo  $category->name;// Category title ?></h1>

<?php

// WP_Query arguments

$args = array (

    'post_type'              => 'gallery',

    'cat'                    => $category->cat_ID,


    'order'                  => 'ASC',

'orderby'                => 'title',

);



// The Query

$query = new WP_Query( $args );



// The Loop

if ( $query->have_posts() ) {

    while ( $query->have_posts() ) {

        $query->the_post();

        ?>
 <div class="col-md-4 col-sm-4 col-xs-12" id="all-post">
 <?php $url = wp_get_attachment_url( get_post_thumbnail_id($query->ID) );     ?>
 <a href="<?php the_permalink(); ?>"><img src="<?php echo $url ?>" class="img-responsive thumb-image-cust thumbnail" alt=""></a>

 </div>
        <?php 

        // You can all phone/ email here

    }

} else {

    // no posts found

}

// Restore original Post Data

wp_reset_postdata();

}
 ?>

1 个答案:

答案 0 :(得分:1)

尝试这种方式,这将显示特定帖子的类别,并检索帖子的条款。

<?php
$postArg = array('post_type'=>'post',
                        'posts_per_page'=>-1,
                        'order'=>'desc',
                      );

        $getPost = new wp_query($postArg);
        global $post;
        if($getPost->have_posts()){
            echo '<ul>';
                while ( $getPost->have_posts()):$getPost->the_post();

                    echo "<h2>".$post->post_title."</h2>";
                    $terms = get_the_terms($post->ID, 'category' );
                    foreach ($terms as $term) {
                        echo "<li>".$term_name = $term->name.'</li>';
                    }
                endwhile;
            echo '</ul>';
        }

?>

您可以通过这种方式在类别名称下显示帖子。

 <?php

     $cat = get_terms('category'); // you can put your custom taxonomy name as place of category.
        foreach ($cat as $catVal) {
            echo '<h2>'.$catVal->name.'</h2>';
            $postArg = array('post_type'=>'post','posts_per_page'=>-1,'order'=>'desc',
                              'tax_query' => array(
                                                    array(
                                                        'taxonomy' => 'category',
                                                        'field' => 'term_id',
                                                        'terms' => $catVal->term_id
                                                    )
                            ));

            $getPost = new wp_query($postArg);
            global $post;

            if($getPost->have_posts()){
                echo '<ul>';
                    while ( $getPost->have_posts()):$getPost->the_post();
                        echo "<li>".$post->post_title."</li>";
                    endwhile;
                echo '</ul>';
            }

        }
    ?>

输出 enter image description here