Wordpress查询类别名称和相关帖子

时间:2017-01-18 19:20:23

标签: wordpress custom-post-type custom-taxonomy

我正在尝试查询与其相关的Wordpress自定义帖子和类别。 查询行为奇怪,显示类别名称和所有海关帖子,甚至与该类别无关(不断重复,直到显示所有类别名称。)

示例图片:Wordpress custom query display

我的查询代码:

<div id="page-content-wrapper">
    <div class="container-fluid">
        <div class="lookbook-header">
            <div class="wrap">
                <p class="text-left">lookbook</p>
            </div>
        </div>

        <?php
        $taxonomy = 'lookbook_categories';
$terms = get_terms($taxonomy);

    $args=array(
     'taxonomy' => 'lookbook_categories'
    'post_type' => 'lookbook',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
        if ( $terms && !is_wp_error( $terms ) ) :
        foreach ( $terms as $term ) { ?>
            <div class="lookbook-category">
                <p class="text-center">
                    <?php echo $term->name; ?>
                </p>
            </div>
            <?php
if( $my_query->have_posts() ) {
        echo '';
        $count=0; 
while ($my_query->have_posts()) : $my_query->the_post();
if($count == 3) {?>
                <div class="row">
                    <?php }
    ?>
                        <div class="col-xs-12 col-sm-12 col-md-4 col-lg-4 no-padding">
                            <div class="lookbook-item">
                                <div class="hvrbox">
                                    <?php 
                                    $image = get_field('lookbook_image');
                                    if( !empty($image) ): ?> <img src="<?php echo $image['url']; ?>" alt="news" class="img-responsive" />
                                        <?php endif; ?>
                                            <div class="hvrbox-layer_top">
                                                <div class="hvrbox-text">
                                                    <div class="separator"></div>
                                                    <h3><?php the_title();?></h3>
                                                    <div class="separator"></div>
                                                    <p>
                                                        <?php the_field('excerpt');?>
                                                    </p>
                                                </div>
                                            </div>

                                </div>

                            </div>
                        </div>
                        <?php    
   $count++; 
        if($count == 3) echo '</div>';
        endwhile;
}
                                      }
                    endif;
wp_reset_query();
?>

                </div>
    </div>

我想显示类别名称+与类别相关的帖子(不是所有帖子一遍又一遍)

1 个答案:

答案 0 :(得分:0)

您希望将数组传递给get_terms,如:

$terms = get_terms( array(
  'taxonomy' => 'lookbook_categories',
  'hide_empty' => false,
) );

您可以在此处详细了解:https://developer.wordpress.org/reference/functions/get_terms/