Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Seb Soithongsuk
Seb Soithongsuk
2,190 Points

How can you display custom content on posts page at top of the body?

It seems that any content you put in the content area of your posts page won't display. Say for example, I want to display an introductory paragraph at the top of my posts page before the list of posts. How could I do this? Do I need to edit some php files?

1 Answer

A.J. Kandy
PLUS
A.J. Kandy
Courses Plus Student 12,422 Points

You'd need to include this content in the template. This static content would be placed at the top of the page before the Loop which displays the posts. This is a common pattern used for category archives where you want to include some descriptive text, images etc.

Now, just sticking static content in an archive template might work, but then it's hard to edit as you need to do it manually. If you want to possibly edit this content down the road using the WP interface, you can simply set this up as a Page whose template contains a Loop for the category of posts you want to display. That way, the page content can still be editable, you can add custom fields etc. So, a hybrid of a static Page and a category archive.

Seb Soithongsuk
Seb Soithongsuk
2,190 Points

Thanks for the reply A.J. I understand the logic, and I found a PHP snippet that looks like it would display the content from the posts page's content field:

if( get_option( 'page_for_posts' ) ) {
    $posts_page = get_page( get_option( 'page_for_posts' ) );
    echo apply_filters( 'the_content', $posts_page->post_content );
}
?>

Bear in mind that my PHP expertise is very limited. I placed this in the index.php (tried in multiple places e.g. in the content div, outside it, at the bottom etc.) but to no avail.

Am I missing something here?