19 Apr 2010

WordPress: Display the contents of static page Posts page

By default, if you choose a static Page for Posts page to show your latest Posts, the original content of that Page will not be displayed.

This is how you can display the content of the Page along with the latest Posts.

Add the following lines to your WordPress Theme’s template index.php file, preferably before the loop:

<?php
if ( 'page' == get_option('show_on_front') && get_option('page_for_posts') && is_home() ) : the_post();
	$page_for_posts_id = get_option('page_for_posts');
	setup_postdata(get_page($page_for_posts_id));
?>
	<div id="post-<?php the_ID(); ?>" class="page">
		<div class="entry-content">	
			<?php the_content(); ?>
			<?php edit_post_link('Edit', '', '', $page_for_posts_id); ?>
		</div>
	</div>	
<?php	
	rewind_posts();
endif;
?>
18 Apr 2010

WordPress: Limit posts per page

Choose and add the following lines to your WordPress Theme’s template functions.php file (For example, functions.php file for a Theme named “default” would probably reside in the directory wp-content/themes/default/functions.php):

  • function limit_posts_per_page() {
    	if ( is_category() )
    		return 2;
    	else
    		return 5; // default: 5 posts per page
    }
    
    add_filter('pre_option_posts_per_page', 'limit_posts_per_page');
    
  • function limit_posts_per_archive_page() {
    	if ( is_category() )
    		set_query_var('posts_per_archive_page', 2); // or use variable key: posts_per_page
    }
    
    add_filter('pre_get_posts', 'limit_posts_per_archive_page');
    

The code example above is for showing 2 posts per page when any Category archive page is being displayed. Same results in two different way.

For more possible page conditions, please refer to the Conditional Tags page in the WordPress Codex.