Grab Static Content

One of WordPress’ more well-known features is the ability to assign static page as a ‘posts’ or blog page (see Settings -> Reading in the Dashboard). However, you always lose the content of the static page being used, which would be ideal for storing a header or intro.

This snippet must be used before the Loop, where the page data is still loaded. It can be used in your theme’s index.php to alter the posts/blog page, but will also work in a custom page template with a Loop with the same effect with the if() clause removed. If you’re familiar with how the $post object works, then $this_page will behave in much the same way.

[sourcecode]<?php

$this_page = $wp_query->get_queried_object(); // Get the current page
$this_page_id = $wp_query->get_queried_object_id(); // Get the current page ID
$posts_page_id = get_option(‘page_for_posts’); // Get the posts page ID

if ( $this_page_id && $this_page_id == $posts_page_id ) { // If the current page has an ID and is the posts page
$updated = ‘Last updated: ‘ . get_the_time($this_page->post_modified); // $this_page is a standard post object
$content = apply_filters(‘the_content’, $this_page->post_content); // Apply ‘the_content’ filters
$content = str_replace(‘]]>’, ‘]]&gt;’, $content);

echo "Last updated: $updated</br>$content";
}

?>[/sourcecode]

To only display the content on the first blog page, add

&& !get_query_var('paged')

to the if() clause.

This post is written by Kawauso. He is a 20-year old university student who writes code for a couple small organizations

Get involved, Vibe with us.

No Vibes for this post.