Static Front and Post pages

Static front and post pages were introduced in WordPress 2.1. For some reason I could not get this working at all and realised that the template directory most likely needed some fresh code. After a little “Googleing” I found out that WordPress by default are trying to pull home.php before index.php when entering your blog (WP installation). Therefore a “hack” should be implemented in this template file.

Creating the necessary code

Create a file called home.php and add the following code inside:

<?php
# Get the page ID's for the front and post pages
$intHome = get_option('page_on_front');
$intPage = get_option('page_for_posts');

# Get the current URI
$strWPPage = "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
# Get your blogs front page URI
$strWPHome = get_bloginfo('wpurl')."/";

# Displays the static front page if set under options --> reading
if (is_home() && !empty($intHome) && $strWPPage == $strWPHome) :

	query_posts('page_id='.$intHome);
	include (TEMPLATEPATH . '/page.php');

# Displays the static posts page if set under options --> reading
elseif (is_home() && !empty($intPage)) :

	query_posts('page_id='.$intPage);
	include (TEMPLATEPATH . '/index.php');

else :

	include (TEMPLATEPATH . '/index.php');

endif;
?>

Adding the new code

Simply upload the new file to your template directory using your favourite FTP client. Finalize the procedure by logging into your WP admin and click Options then Reading and adjust the Static pages options listed the way you want.

Hint: By adding a blank page called e.g. “Blog” you would get a very logical link to your static post listings page…

About Author