我在WordPress管理面板中设置了每页显示10篇文章,但我想在第一页和我的主页上显示11篇文章。所以我想在主页上显示11篇文章,对于所有其他页面,我想显示10篇文章。
我正试图用这段代码实现它,但是无论页码如何,_home()总是返回true。
function home_custom_query( $query ) {
if ( is_home() )
{
$query->query_vars['posts_per_page'] = 11;
}
return $query;
}
add_filter( 'pre_get_posts', 'home_custom_query' );
但上面的代码改变了包括主页在内的所有页面的帖子数量。
我也尝试了这个检测页码并尝试如下
function home_custom_query( $query ) {
if ( is_home() && $query->query_vars['paged'] == 0)
{
$query->query_vars['posts_per_page'] = 11;
}
return $query;
}
add_filter( 'pre_get_posts', 'home_custom_query' );
它将主页的每页帖子更改为11个,但在所有其他页面上不显示任何内容。
我还尝试了cbnet每页不同帖子的插件,它实现了这个目的,但它增加了一些我不想使用的额外功能。我需要一个简单的方法来解决我的问题。我相信会有一个干净整洁的方法来解决这个问题。
您的模板必须在某个地方有一个循环,您可以在其中执行查询帖子。您可以在其中显示参数。她是一个榜样。
<?php
$page_num = $paged;
if ($pagenum='') $pagenum =1;
if(is_home) { $showposts = 11 } else { $showposts = 10 }
query_posts('showposts=' .$showposts. '&paged='.$page_num);
?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
// WordPress loop
<?php endwhile; ?>
<?php endif; ?>
我这样做了,可能对你也有帮助。
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$page_num = $paged;
if( $paged >1 )
$showposts = 10;
else
$showposts = 11;
query_posts('showposts=' .$showposts. '&paged='.$page_num);
?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
// WordPress loop
<?php endwhile; ?>
<?php endif; ?>