Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am new to the world of WordPress.
I own a new site and I'd like my home page to display the latest news and articles (which I accomplished to set in Settings > Reading), but I'd like another page to display all posts (not the whole post but a summary) without the need to press "previous page".

e.x.
If I have 8 posts and I set maximum posts to be 4 in the settings menu, my home will have 4 posts. But I'd like the other page to have all 8 posts in summary without the need to press previous page to read other posts.
How can that be done?

share|improve this question
add comment

1 Answer 1

I would recommend creating a custom template and utilize a custom query to do this.

This is because changing the maximum posts to any number will affect every page that has a list of posts unless you specify otherwise.

By making your own query you can set the number of posts you want by using something like:

get_posts( 'posts_per_page=4' )

So for example you can create a custom template file called 'template-four-posts.php' and put this in your theme's folder content/themes/[your-theme-name]/ .

Next open this file and add in these lines of code.

These first few lines will pull in your header file as well as name our template.

<?php
/*
Template Name: Four Post Count
*/
get_header(); ?>

This next set of code is to prepare our query by asking for 4 posts of the post type 'post' and ordering it by post date in descending order:

<?php 
$recent_posts_query = array(
'numberposts' => 4,
'post_type'   => 'post',
'orderby'     => 'post_date',
'order'           => 'DESC',
);

Next we use our query to find the most recent 4 posts:

$recent_posts = get_posts( $recent_posts_query );

Then we run through a loop taking each post and adding it to the page. This code will show each post and display it's date, title and link to the post:

foreach( $recent_posts as $post ) : setup_postdata($post); ?>
<div class="post">
    <p class="date"><?php the_time('M j, Y'); ?></p>
    <p><a class="title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
</div><!-- .post -->
<?php
endforeach;
?>

This last piece is to call your footer file and add that code to your page:

<?php get_footer(); ?>

Altogether your code should look like this:

    <?php
    /*
    Template Name: Four Post Count
    */
    get_header(); ?>
<?php 
$recent_posts_query = array(
    'numberposts' => 4,
    'post_type'   => 'post',
    'orderby'     => 'post_date',
    'order'           => 'DESC',
);

$recent_posts = get_posts( $recent_posts_query );

foreach( $recent_posts as $post ) : setup_postdata($post);      
    ?>
    <div class="post">
        <p class="date"><?php the_time('M j, Y'); ?></p>
        <p><a class="title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
    </div><!-- .post -->
    <?php       
endforeach;
?>
 <?php get_footer(); ?>

Next log into the backend of your site and click on a page. Next select find the 'Page Attributes' meta box on the right hand side and select the template named 'Four Post Count' from the template drop down and click update.

Then go visit this page and you should see the list of page.

Keep in mind the way this looks may differ depending on the template you are using and what code is in your header.php or footer.php files.

share|improve this answer
1  
Forget that query_posts even exists. Please. That aside, your answer applies, but please either hook into pre_get_posts or make use of WP_Query directly. –  Johannes Pille Jul 10 '13 at 16:22
1  
I second JohannesPille. Please do not encourage people to use query_posts ! –  s_ha_dum Jul 10 '13 at 16:24
    
thanks for your quick answer but can you please tell me in which file should i add this code? –  randomuser1234 Jul 10 '13 at 16:38
    
Ooops! Thanks for catching that. Yes use get_posts :) my bad. –  Alex Jul 10 '13 at 16:47
    
wrong. You can get 404 with this solution too. –  JMau Jul 10 '13 at 16:47
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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