0

I have customised index.php to display 6 posts:

<?php
// Get header
get_header(); ?>

<div id="primary">
<?php // Begin paginate and loop
  $temp = $wp_query; 
  $wp_query = null; 
  $wp_query = new WP_Query(); 
  $wp_query->query('showposts=6'.'&paged='.$paged); 
  if ($wp_query->have_posts()) : while ( have_posts() ) : $wp_query->the_post(); 
?>
<article>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> 
<?php the_excerpt(); ?>
</article>

<?php endwhile; // End while ?>

<nav>
    <?php previous_posts_link('&laquo; Newer Posts') ?>
    <?php next_posts_link('Older Posts &raquo;') ?>
</nav>

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

<?php else: ?>
<p><?php _e('Please add some posts.'); ?></p>
<?php endif; // End loop and primary ?>
</div>

<?php get_sidebar(); ?>
<?php get_footer(); ?>

For this to work correctly, I need to set "Blog pages show at most" from 10 posts to 6.

As the theme I'm building will be used on a multisite environment, I'd like to set the default reading setting to 6 posts. Is it possible to change the default setting via functions.php?

I've taken a look at options-reading.php within the core of WordPress. Line 126 shows the following:

<?php _e( 'Blog pages show at most' ); ?>

Changing this value obviously doesn't update the database value without updating the settings page within WP Admin.

If you could point me in the right direction, that would be great.

1

You can use the wpmu_new_blog action to automatically set the default to 6 for new installs:

function wpse_139900_default_posts_per_page( $blog_id ) {
    update_blog_option( $blog_id, 'posts_per_page', 6 );
}

add_action( 'wpmu_new_blog', 'wpse_139900_default_posts_per_page' );

If you don't want your users to be able to change this setting, you can just override it instead:

function wpse_139900_posts_per_page() {
    return 6;
}

add_filter( 'pre_option_posts_per_page', 'wpse_139900_posts_per_page' );

Either way, you should never use your own query on top of the default loop - it's just unnecessary overhead that can be avoided by tweaking the main query.

| improve this answer | |
  • Thanks @TheDeadMedic. I adapted this paginate technique to include an else statement: css-tricks.com/snippets/wordpress/paginate-custom-post-types. If you could link me to a better technique, I'd appreciate it :) – Sam Apr 1 '14 at 18:25
  • 1
    There's simply no need for the custom query. Just use if ( have_posts() ) : while ( have_posts() ) : the_post() without any custom WP_Query. The code I've provided will automatically handle the "6 per page" requirement :) – TheDeadMedic Apr 1 '14 at 19:39
1

You have a slight syntax error here. You forgot $wp_query-> before the second have_posts()

if ($wp_query->have_posts()) : while ( have_posts() ) : $wp_query->the_post();

to

if ($wp_query->have_posts()) : while ( $wp_query->have_posts() ) : $wp_query->the_post();
| improve this answer | |
  • 1
    Pleasure, glad I could be of some assistance. Enjoy – Pieter Goosen Apr 1 '14 at 18:21

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.