获取特定类别的所有帖子 - Wordpress

时间:2018-06-13 02:53:11

标签: php wordpress wordpress-theming

我正在尝试获取“商家”类别的所有帖子,并将其显示在文章模板列表中。但是,查询将返回所有类别的所有帖子。

<?php
    $args = array(
        'posts_per_page'   => -1,
        'post_type'        => 'post',
        'category-name'    => 'Business'
    );
    $the_query = new WP_Query( $args );

?>
<?php get_header(  ); ?>
<div class="items-leading">
    <?php while ( $the_query->have_posts() ): $the_query->the_post();?>
<!--Start to display the article-->
<article class="leading-0 item visible-first" id="item-8">
                <!-- Intro image -->
                <figure class="item_img img-intro img-intro__left">
                        <a href="<?php the_permalink(  );?>">
                            <img src="" alt="">
                        </a>
                </figure>

                <!--  title of the article -->
                <header class="item_header">
                <h5 class="item_title heading-style-5 visible-first">
                <a href="<?php the_permalink(  );?>">
                <span class="item_title_part_0 item_title_part_odd item_title_part_first_half item_title_part_first"><?php the_title( );?></span> 
                </a></h5></header>

                <!-- info about the time published and author -->
                <div class="item_info">
                    <dl class="item_info_dl">
                        <dt class="article-info-term"></dt>

                                <dd>
                            <time class="item_published">
                                <?php the_date( );?>    </time>
                        </dd>
                                <dd>
                            <address class="item_createdby">
                            <?php the_modified_author();?>  
                            </address>
                        </dd>
                                <dd>
                            <div class="komento">
                                            </div>
                        </dd>
                            </dl>
                </div>
<!-- Introtext -->
<div class="item_introtext">
    <p>
    <?php echo wp_trim_words( get_the_content(  ), 40, '...')?>
    </p>
</div>

<!-- info BOTTOM -->

<!-- More -->
<a class="btn btn-info" href="<?php the_permalink(  ); ?>">
    <span>
        Read more   
    </span>
</a>
</article>
<div class="clearfix"></div>

<?php endwhile; ?>
</div>
<!-- end items-leading -->
<?php get_footer();?>

代码中出现了什么问题?但我不明白,因为这个查询是基于我在互联网上找到的。 如果有任何帮助,我将不胜感激。

3 个答案:

答案 0 :(得分:0)

我认为$ args存在问题,试试这个:

$args = array(
  'posts_per_page'   => -1,
  'post_type'        => 'post',
  'category_name'    => 'Business'
);

答案 1 :(得分:0)

没有任何查询问题,只是简单的愚蠢错误。

<?php get_header(  ); ?>

将在文件和文件的初始位置然后放置您的查询arg&amp;所有

答案 2 :(得分:0)

嗯......我不确定,但不知怎的,我改变了查询,这运行良好._。 ?

太神奇了。

<?php get_header(  ); ?>
<?php
    $args = array(
        'posts_per_page'   => -1,
        'post_type'        => 'post',
        'cat'      => '8'
      );
    $the_query = new WP_Query( $args );

?>