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/

22 Comments Leave a comment

  1. HAROLD says:

    I EFFIN LOVE YOU!!!

    THAAANKS!

  2. Adam says:

    Thanks, this was driving me crazy.

  3. Paul B. Taubman, II says:

    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.

  4. Erik Teichmann says:

    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 says:

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

  5. Adrian says:

    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

  6. ( jason ) says:

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

  7. jomachi says:

    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.

  8. Gareth Joyce says:

    @Jomachi

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

    			
    				<div id="post-" class="page">
    					
    						
    						
    						
    					
    				
    			
    

  9. Gareth Joyce says:

    No it didn’t!!

    Try adding this:

    $title = get_the_title($page_for_posts_id);

    Then echoing out $title where you need it.
    :)

  10. Arshaan says:

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

  11. avremy cohen says:

    you guys are great

  12. Jody Heatth says:

    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/

  13. Kelly says:

    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

  14. Mehrshad says:

    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 says:

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

  15. JB says:

    RAD. this worked for me. Thanks for posting

  16. Dan Hansen says:

    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.

  17. Jens Mikkelsen says:

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

  18. yasmina says:

    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 says:

      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.

  19. yasmina says:

    Ohh too bad but thanks for you respond