0

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.

  • 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 Feb 22 '14 at 10:20
0

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;
}
  • Thanks for your response. About your updated solution-- the blog page is not home, so then should I change "is_home" to something else? – Jaeeun Lee Feb 24 '14 at 17:40
  • I don't know the structure of your theme, but copy it as is and write what is the result :) – NoSense Feb 24 '14 at 19:15
  • I did, but the blog just shows all the posts. – Jaeeun Lee Feb 24 '14 at 20:01
  • if your blog page is a static page with template you could change is_home with is_page(PUT HERE THE ID OF THE PAGE) – NoSense Feb 24 '14 at 20:06
  • I set the id like this ' if ( is_page(14) ) { // Display only 1 post for the original blog archive $query->set( 'posts_per_page', 1 ); return; }' but the blog shows everything. – Jaeeun Lee Feb 24 '14 at 20:38
0

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.

  • Thanks for your response. But the blog page is not home, so then should I change "is_home" to something else? – Jaeeun Lee Feb 24 '14 at 17:57
  • Updated my answer. Mark this as the accepted answer if it solves your problem. – Brad Dalton Feb 24 '14 at 23:08
  • Your posts page, according to your Reading Settings is your home page even if its not your front page. – Brad Dalton Feb 24 '14 at 23:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.