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
  • Likely, you have a plugin or theme code that is conflicting... perhaps something is filtering pre_get_posts. Try disabling plugins. Then try switching to a default theme to isolate the problem. You shouldn't need to manually set the posts_per_page as the option you've chose in the reading settings is the default for WP_Query. – helgatheviking Commented Feb 22, 2014 at 10:20
Add a comment  | 

2 Answers 2

Reset to default 1

No 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