Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 953
Антон Слепцов Avatar asked Sep 13 '26 05:09

Антон Слепцов


1 Answers

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.

like image 70
acobster Avatar answered Sep 17 '26 00:09

acobster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!