我的单曲中有以下代码。php页面:
UPDATED CODE
<?php if (have_posts()) : the_post();
$post_id = get_the_ID();
if ( is_single() ) {
$cats = get_the_category();
$cat = $cats[0];
} else {
// category archives
$cat = get_category( get_query_var( \'cat\' ) );
}
$cat_id = $cat->cat_ID;
$cat_name = $cat->name;
$cat_slug = $cat->slug;
?>
<?php query_posts(\'cat=\'.$cat_id); ?>
<?php if (have_posts()) : ?>
<div id="sidebar">
<h3><?php echo get_cat_name($cat_id); $theCount = 0; ?></h3>
<ul class="info-list">
<?php while (have_posts()) : the_post(); $theCount += 1; echo \'debug>\' . $theCount; ?>
<li <?php if ($post_id == get_the_ID()) {echo \'class="active"\';} ?>>
<a href="<?php the_permalink(); ?>">
<span><?php the_title(); ?></span>
</a>
</li>
<?php endwhile; ?>
</ul>
</div>
<?php endif; ?>
<?php endif; ?>
问题是,它似乎只抓取了该类别中的最后10篇文章,我知道我有16篇文章可供侧边栏显示。
在代码中,我可以在哪里找到限制返回值的地方??
UPDATE
<?php if (have_posts()) : the_post();
$post_id = get_the_ID();
if ( is_single() ) {
$cats = get_the_category();
$cat = $cats[0];
} else {
// category archives
$cat = get_category( get_query_var( \'cat\' ) );
}
$cat_id = $cat->cat_ID;
$cat_name = $cat->name;
$cat_slug = $cat->slug;
$cat_query_args = array(
// Include posts from category with ID $cat_id
\'cat\' => $cat_id,
// Include all posts
\'posts_per_page\' => -1
);
$cat_query = new WP_Query( $cat_query_args );
if ( $cat_query->have_posts() )
?>
<div id="sidebar">
<h3><?php echo get_cat_name($cat_id); $theCount = 0; ?></h3>
<ul class="info-list">
<?php
while $cat_query->have_posts() : $cat_query->the_post();
$theCount += 1; echo \'debug>\' . $theCount;
?>
<li <?php if ($post_id == get_the_ID()) {echo \'class="active"\';} ?>>
<a href="<?php the_permalink(); ?>">
<span><?php the_title(); ?></span>
</a>
</li>
<?php
endwhile;
?>
</ul>
</div>
<?php
endif;
?>
<?php endif; ?>
<?php wp_reset_query(); ?>
最合适的回答,由SO网友:Chip Bennett 整理而成
你的问题是你正在使用query_posts()
, 当您需要使用WP_Query()
. 要显示所有帖子(通过忽略分页,请使用posts_per_page
parameter:
$cat_query_args = array(
// Include posts from category with ID $cat_id
\'cat\' => $cat_id,
// Include all posts
\'posts_per_page\' => -1
);
$cat_query = new WP_Query( $cat_query_args );
if ( $cat_query->have_posts() ) :
?>
<div id="sidebar">
<h3><?php echo get_cat_name($cat_id); $theCount = 0; ?></h3>
<ul class="info-list">
<?php
while ( $cat_query->have_posts() ) : $cat_query->the_post();
$theCount += 1; echo \'debug>\' . $theCount;
?>
<li <?php if ($post_id == get_the_ID()) {echo \'class="active"\';} ?>>
<a href="<?php the_permalink(); ?>">
<span><?php the_title(); ?></span>
</a>
</li>
<?php
endwhile;
?>
</ul>
</div>
<?php
endif;
// Be kind; rewind
wp_reset_postdata();
SO网友:s_ha_dum
使用query_posts
只有当您故意干扰主查询时,才应该这样做,而这几乎是不应该的。
使用get_posts
或者创造一个新的WP_Query
对象。
$my_posts = get_posts(
\'cat=\'.$cat_id,
\'posts_per_page=50\'
);
如果您的
$cat_id
是正确的,应该可以。然后像这样绕过去(
from the Codex):
$args = array( \'numberposts\' => 3 );
$lastposts = get_posts( $args );
foreach($lastposts as $post) : setup_postdata($post); ?>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_content(); ?>
<?php endforeach; ?>
或使用
WP_Query
喜欢(
also from the Codex):
// The Query
$the_query = new WP_Query( $args );
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
echo \'<li>\';
the_title();
echo \'</li>\';
endwhile;