• Home  / 
  • WordPress
  •  /  WordPress Tip #1 – Page Content on the Posts Page

Make sure you don't miss the latest insights and tips. Click here to sign up for my mailing list now! »

WordPress Tip #1 – Page Content on the Posts Page

If you’ve used WordPress a fair bit, you’re probably familiar with the practice of using a ‘static page’ as your home page, and specifying another page to act as your ‘posts page’ (which displays a list of your posts).

If you don’t know what I’m talking about, this WordPress Codex article explains it clearly.

One important point that the above-mentioned codex article emphasises is ‘DO NOT add content to the Blog Page. Leave it blank. Any content here will be ignored’

This means that any content you add to your ‘posts page’ will not show up on the published page. It will only show the list of posts.

This has frustrated me for years, as I often want to include a sort of introduction at the top of the blog, and until recently I have resorted to hard-coding this into the theme.

However, I recently stumbled upon the following solution, which allows the contents of the ‘post page’ to be output as well as the list of posts.

Just add the following code to your index.php file in your theme directory:

<?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;
?>

See our articles page for an example. The introductory text above the posts list comes from the WordPress page.

Source: http://zeo.my/

About the author

Dan Johnson

Dan used to be a WordPress developer at WDFS, but now he has gone off to pursue his passion for art, and he blogs about making a living from creativity at Right Brain Rockstar

HAROLD - 3 years ago

I EFFIN LOVE YOU!!!

THAAANKS!

Adam - a couple of years ago

Thanks, this was driving me crazy.

Paul B. Taubman, II - a couple of years ago

I have been looking for something like this, on and off, for months now! Thanks for posting this.

On a similar note… I would install WP in the root. I would use the static page to create an ‘intro’ page, or to have my squeeze page on the root. My blog page would be titled, Blog so it looked as if it was installed in a sub-directory

Recently, I started to leave the root of a domain open, and installing WP in a sub-directory (/blog/ as before). However, my root would be a .html file

Any thoughts on this practice?

Thanks!
Be Well.
Paul.

Erik Teichmann - a couple of years ago

Thanks for this, very helpful!

FYI, if you want to include the title from your static content page, you’ll have to do it like so:

<pre><code></code></pre>

If you just use the_title(), WordPress will still display the title of the first post.

    Douglas Rogers - a couple of years ago

    Your solution didn’t show up. What exactly will you have to do in order for it to show?

Adrian - a couple of years ago

That’s awesome, thanks

I used the above to add content to the bottom of my Posts Page (adding the code in just above the closing content div), essentially adding what looks similar to an Archives widget underneath the latest posts (client wanted the archives to appear in the page, as opposed to the sidebar) and with a bit of styling, will look v.nice

( jason ) - a couple of years ago

Once I figured out what hook to use in my child theme, it worked like a charm. Thank you!

jomachi - a couple of years ago

Very effective and useful tip. But how show the page title ? Because adding the the_title() function show the first post title instead of the page title. Thanks.

Gareth Joyce - a couple of years ago

@Jomachi

This is what I did to display the_title() hope my code displays.

			
				<div id="post-" class="page">
					
						
						
						
					
				
			
Gareth Joyce - a couple of years ago

No it didn’t!!

Try adding this:

$title = get_the_title($page_for_posts_id);

Then echoing out $title where you need it.

:)

Arshaan - last year

Thanks a lot. Had been looking for this all day :)

avremy cohen - last year

you guys are great

Jody Heatth - last year

Thanks for your post. I’m new to wordpress. Although this your wasn’t exactly what I was looking for, it helped get me thinking in the right direction to find this other article which was exactly what I was looking for. It’s a simple way to top post any blog post you like by using only the edit visibility section drop down (right above the update button):
http://en.support.wordpress.com/posts/post-visibility/

Kelly - last year

Ok totally new to hosted wordpress blog. I am completely missing the details here. I simply added this section of code to my theme index.php and nothing changed.

Please give me details!

Thanks
Kelly

Mehrshad - last year

Dear Sir
Hi. Tank you for this code. I’m very new in this field and if you give me a simple explanation it makes me very appreciated. I use a child-theme twentytwelve for my website. For application of this code I mad an index.php file in the child’s directory and added this code to it (at the very top of index.php). Subsequently, website posts page just showed the static text without any style on a totally white page and all posts disappeared. Would you do me a favour please? Which parts of the process I did were incorrect?
Regards

    Ben Hunt - last year

    Hi Mershad. Unfortunately, Dan, who wrote this post, has moved on to great things at WooThemes.

JB - last year

RAD. this worked for me. Thanks for posting

Dan Hansen - last year

For anyone having trouble getting post data to work other than the_content, the reason is actually documented in the WP codex: https://codex.wordpress.org/Function_Reference/setup_postdata . It’s an issue with how setup_postdata() likes working with the actual global $post variable.

To fix it and be able to use things like the_ID() and the_title(), change this line:

setup_postdata(get_page($page_for_posts_id));

to read:

global $post; //you may not need this
$post = get_page(page_for_posts_id);
setup_postdata($post);

And the other functions should start working.

Jens Mikkelsen - last year

Nice thank you for your tip :)
Weird that its isn’t default in WordPress 3.6

yasmina - 11 months ago

I am seriously looking crazy for this. But where the heck do you out them? I am using prose theme. I can get my site go live because of this. Please respond . Do I have to put it in the style sheet CSS and where exactly. I don’t know where the home.php is. On the right side all I see is
Function.php. And landing.php and initial.php
I’ve been looking for days and and I am tired.
So please help :)

    Ben Hunt - 11 months ago

    Hi Mina. Dan, the author of this post now works at WooThemes, and I don’t have the particular expertise to be able to help you, sorry.

yasmina - 11 months ago

Ohh too bad but thanks for you respond

Comments are closed