admin管理员组文章数量:1334806
I set the number of posts to show on my blog to 1, but the blog is showing all the posts I have. I tried to insert a custom query into my index.php, but it just made one post show up endlessly. This is my index.php:
<?php get_header(); ?>
<?php if ( have_posts() ) : ?>
<div id="content" class="group">
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_sidebar(); ?>
<div id="post_column">
<div class="post_entry group">
<div class="post_header"><h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
<?php get_footer(); ?>
I'd appreciate your help.
I set the number of posts to show on my blog to 1, but the blog is showing all the posts I have. I tried to insert a custom query into my index.php, but it just made one post show up endlessly. This is my index.php:
<?php get_header(); ?>
<?php if ( have_posts() ) : ?>
<div id="content" class="group">
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_sidebar(); ?>
<div id="post_column">
<div class="post_entry group">
<div class="post_header"><h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
<?php get_footer(); ?>
I'd appreciate your help.
Share Improve this question asked Feb 22, 2014 at 4:49 Jaeeun LeeJaeeun Lee 4251 gold badge5 silver badges14 bronze badges 1 |2 Answers
Reset to default 1No need to edit your parent themes files as its bad practice.
You can use this code in your child themes functions file
add_action( 'pre_get_posts', 'wpsites_limit_posts' );
function wpsites_limit_posts( $query ) {
if( $query->is_main_query() && !is_admin() && is_home() ) {
$query->set( 'posts_per_page', '1' );
}
}
Won't work when admin is logged in.
Change the is_home() conditional tag to the archive you want to limit posts per page.
I'm newbie in WordPresss, but try to change your code to this:
<?php get_header(); ?>
<?php if ( have_posts() ) : ?>
<div id="content" class="group">
<?php get_sidebar(); ?>
<?php /* Start the Loop */ ?>
<?php $myQuery = new WP_Query(array('posts_per_page' => 1));
while($posts_to_update ->have_posts()) : $posts_to_update ->the_post(); ?>
<div id="post_column">
<div class="post_entry group">
<div class="post_header"><h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
<?php get_footer(); ?>
UPDATE:
Another solution is to add this code in functions.php :
if ( is_home() ) {
// Display only 1 post for the original blog archive
$query->set( 'posts_per_page', 1 );
return;
}
本文标签: Reading Setting Not Working for Number of Posts
版权声明:本文标题:Reading Setting Not Working for Number of Posts 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742263316a2442920.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
pre_get_posts
. Try disabling plugins. Then try switching to a default theme to isolate the problem. You shouldn't need to manually set theposts_per_page
as the option you've chose in the reading settings is the default forWP_Query
. – helgatheviking Commented Feb 22, 2014 at 10:20