从自定义Category y.php的循环中排除类别和帖子

时间:2018-12-08 作者:Jamdev

所以我创建了一个自定义类别。php中有以下循环:

<div id="category__product-grid" class="row">
<?php

// The Loop
while ( have_posts() ) : the_post(); ?>

<div class="col-md-4 col-product-item">
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><div class="cat__featured-img"><?php the_post_thumbnail(); ?></div></a>
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
</div>

<?php endwhile; 

else: ?>
<p>Sorry, no posts matched your criteria.</p>
</div>  
我绞尽脑汁,试图弄明白如何才能从这个循环中排除所需的类别以及所需的帖子。非常感谢您的帮助!

1 个回复
最合适的回答,由SO网友:Jamdev 整理而成

以下是正确的方法:

<div id="category__product-grid" class="row">
<?php query_posts( array( \'cat\' => array(6,-45), \'post__not_in\' => array(-598), \'orderby\' => \'title\', \'order\' => \'ASC\' ) ); ?>

<?php

// The Loop
while ( have_posts() ) : the_post(); ?>

<div class="col-md-4 col-product-item">
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><div class="cat__featured-img"><?php the_post_thumbnail(); ?></div></a>
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
</div>

<?php endwhile; 

else: ?>
<p>Sorry, no posts matched your criteria.</p>
</div>