Limit Loop to One Category

时间:2013-06-11 作者:drewrowalnd

我试图将我的循环限制为一个类别。这是循环:

<?php
$counter = 1; //start counter

$grids = 3; //Grids per row

global $query_string; //Need this to make pagination work


/*Setting up our custom query (In here we are setting it to show 12 posts per page and eliminate all sticky posts) */
query_posts($query_string . \'&caller_get_posts=1&posts_per_page=9&category=5\');



if(have_posts()) :  while(have_posts()) :  the_post(); 
?>
<?php
//Show the left hand side column
if($counter == 1) :
?>
            <div class="four columns">
                <div class="postimage">
                    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail(\'home-thumb\'); ?></a>
                </div>
                <h3 style="text-align:center;"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
                <p><?php the_excerpt(); ?></p>
            </div>
<?php
//Show the middle column
elseif($counter == $grids) :
?>
<div class="four columns">
                <div class="postimage">
                    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail(\'home-thumb\'); ?></a>
                </div>
                <h3 style="text-align:center;"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
                <p><?php the_excerpt(); ?></p>
            </div>
<?php
//Show the right hand side column
elseif($counter == $grids) :
?>
<div class="four columns">
                <div class="postimage">
                    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail(\'home-thumb\'); ?></a>
                </div>
                <h3 style="text-align:center;"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
                <p><?php the_excerpt(); ?></p>
            </div>          

<div class="clear"></div>
<?php
endif;
?>

<?php
endwhile;
//Pagination can go here if you want it.
endif;
?>
这是页面温度。

<?php
/*
Template Name: Web Design
*/
 get_header(); ?>
<hr>

    <div  id="content" class="twelve columns">

        <?php get_template_part(\'loop\', \'web\'); ?>

    </div>

<?php get_footer(); ?>
它不起作用。请帮忙。谢谢

1 个回复
SO网友:Milo

没有category 的参数query_posts, 类别ID应通过cat.

However, 如果这是一个附加查询,请使用WP_Query 而不是query_posts. 如果这是主要查询,请参阅pre_get_posts 更改主要查询参数。

有关更多信息,请参阅When to use WP_query(), query_posts() and pre_get_posts.

结束

相关推荐

For Each +1 Wordpress Loop

我用一个循环来制作一个滑块。我的代码是<?php query_posts( array( \'post_type\' => \'my_gallery\', \'author\'=>$author->ID ) ); while( have_posts() ) : the_post(); $imagethum = eh_postImage(115, 70); ?> <a data-slide-