首页 文章

在wordpress category.php中的服装查询

提问于
浏览
1

我在服装模板中为显示类别存档创建了category.php .

在类似页面链接中,如下所示:http://www.example.com/category/cat1/

通过这些代码,它可以显示 cat1 的最后一项

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
// Some template code
<?php endwhile; ?>
<?php endif; ?>

但是当我尝试通过 WP_Queryquery_posts 而不是 cat1 的内容自定义查询时,它会显示所有类别网站的内容

<?php query_posts( 'posts_per_page=30' ); ?>    
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    // Some template code
    <?php endwhile; ?>
    <?php endif; ?>

什么是理由和解决方案?

1 回答

  • 1

    您必须在查询中定义cat .

    这是你的答案:

    <?php
    $args = array(
        'cat' => get_query_var('cat'),
            'posts_per_page' => 30
    );
    $recent = new WP_Query($args); while($recent->have_posts()) : $recent->the_post();?>
     //some template code
    
    <?php endwhile; ?>
    

相关问题