在我的循环中。php(见下文)我基本上有一个条件,如果页面没有分页(即如果URL看起来不像这样:example.com/page/2),那么显示2篇带有“特色内容”类别的帖子。。。否则,如果页面已分页,则显示5篇文章。这很好,只是它不能正确地查询“特色内容”类别,因为标记了该类别的帖子并不总是显示在循环提要中。
我希望我正确地阐述了这个问题,如果没有,请让我知道,我会澄清。
<!-- Featured Content Slider -->
<?php include(\'includes/featured-content-slider.inc.php\'); ?>
<!-- /Featured Content Slider -->
<!-- 960 16 Column Grid -->
<div class="container_16" <?php if (!is_paged()) { echo "id=home";} ?>>
<!-- Featured News -->
<section class="grid_10 featured-news" <?php if (is_paged()) {echo "id=margin-top-120-o";}?>>
<!-- Featured News Heading -->
<h1><span class="biography">Featured Content</span></h1>
<!-- /Featured News Heading -->
<!-- Featured News Loop -->
<?php
if (!is_paged()) {
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$args=array(
\'post_type\'=>\'post\',
\'category_name\' => \'featured-content\',
\'posts_per_page\' => 2,
\'paged\'=>$paged
);
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query($args);
} else {
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$args=array(
\'post_type\'=>\'post\',
\'category_name\' => \'featured-content\',
\'posts_per_page\' => 5,
\'paged\'=>$paged
);
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query($args);
}
if (function_exists(\'wp_pagenavi\')) { wp_pagenavi(); }
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();
?>
<!-- /Featured News Loop -->
<!-- Post -->
<article class="post" id="post-<?php the_ID(); ?>" <?php post_class(); ?> >
<!-- Featured News Title -->
<span><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( \'%s\', \'twentyten\' ), the_title_attribute( \'echo=0\' ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
</span>
<!-- /Featured News Title -->
<!-- Featured News Meta -->
<p class="featured-news-post-meta">By <span class="featured-news-author"><?php echo get_the_author(); ?></span> / <?php echo get_the_date(\'m.d.Y\'); ?></p>
<!-- /Featured News Meta -->
<!-- Featured News Thumbnail -->
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(array(640,320)); ?></a>
<!-- /Featured News Thumbnail -->
<!-- Featured News Excerpt -->
<p class="featured-news-excerpt"><?php the_excerpt(); ?></p>
<!-- /Featured News Excerpt -->
<!-- Featured News Social Links -->
<?php include(\'includes/social.inc.php\'); ?>
<!-- /Featured News Social Links -->
</article>
<!-- /Post -->
<!-- Featured News Loop -->
<?php
endwhile; endif;
/* PageNavi at Bottom */
if (function_exists(\'wp_pagenavi\')){wp_pagenavi();}
$wp_query = null;
$wp_query = $temp;
wp_reset_query();
?>
<!-- /Featured News Loop -->
<!-- Pagination -->
<?php if ($wp_query->max_num_pages > 1) : ?>
<div class="grid_10 pagination older-news">
<?php next_posts_link( __( \'<span class="arrow">←</span> Older\', \'twentyten\' ) ); ?>
<div class="pagination newer-news">
<?php previous_posts_link( __( \'Newer <span class="arrow">→</span>\', \'twentyten\' ) ); ?>
</div>
</div>
<?php endif; ?>
<!-- /Pagination -->
</section>
<!-- /Featured News -->
<!-- What\'s Happening -->
<div <?php if (is_paged()) {echo "id=margin-top-120-o";}?>>
<?php include(\'includes/whats-happening.inc.php\'); ?>
</div>
<!-- /What\'s Happening -->
</div>
<!-- /960 16 Column Grid -->
SO网友:helgatheviking
我不确定使用$paged变量想要实现什么。看起来你可以简化你的查询
$posts_per_page = is_paged() ? 5 : 2;
$args=array(
\'post_type\'=>\'post\',
\'category_name\' => \'featured-content\',
\'posts_per_page\' => $posts_per_page
);
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query($args);
编辑、添加偏移量(并修复语法错误):
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$posts_per_page = !is_paged() ? 5 : 2;
$offset = is_paged() ? 5 + 2*($paged-2) : 0;
$args=array(
\'post_type\'=>\'post\',
\'category_name\' => \'featured-content\',
\'posts_per_page\' => $posts_per_page,
\'offset\' => $offset
);
$featured = new WP_Query($args);
if($featured->have_posts()):
echo \'<ul>\';
while ($featured->have_posts()): $featured->the_post();
echo \'<li>\' . get_the_title(). \'</li>\';
endwhile;
echo "</ul>";
endif;
如果您没有大量的特色项目,则在返回分页时可能会用完。