wordpress wp query posts per page default

Here is the code:

<?php if ( have_posts() ) : ?>

                <?php
                    $temp = $wp_query;
                    $wp_query= null;
                    $wp_query = new WP_Query();
                    $wp_query->query('showposts=5'.'&paged='.$paged);
                    while ($wp_query->have_posts()) : $wp_query- >the_post();
                ?>

                    <?php get_template_part( 'content', get_post_format() ); ?>

                <?php endwhile; ?>

                <?php higher_content_nav( 'nav-below' ); ?>


        <?php $wp_query = null; $wp_query = $temp;?>

        <?php endif; ?>

How can I show posts per page not 5 but default? I mean post that we can set at back end at Reading Settings, Blog pages show at most - number of post.

Upvotes: 3

Views: 20336

Answers (4)

acobster
acobster

Reputation: 1657

The safest way is to call get_option, which gets the value directly from the database:

$showposts = get_option('posts_per_page');
$wp_query->query('showposts='.$showposts.'&paged='.$paged);

Global variables like $numposts are not guaranteed to be set.

For future reference, you can usually determine what to pass to get_option by finding the name attribute of the setting's <input> in WP Admin.

Upvotes: 4

Here is the solution:

global $numposts;
$wp_query->query( 'showposts='.$numposts.'&paged='.$paged );

Upvotes: 0

RRikesh
RRikesh

Reputation: 14411

You are using WP_Query(), it does not have any default value for the number of posts per page.

You should however note that showposts has been replaced by posts_per_page in your code:

$wp_query->query('showposts=5'.'&paged='.$paged);

Upvotes: 0

Nina
Nina

Reputation: 103

<?php query_posts( 'posts_per_page=5' ); ?>

<?php if ( have_posts() ) : ?>
    <?php while ( have_posts() ) : the_post(); ?>
        <?php get_template_part( 'content', get_post_format() ); ?>
    <?php endwhile; ?>
<?php endif; ?>

Upvotes: 2

Related Questions