Last active
July 13, 2017 15:58
-
-
Save morgyface/0c1641b72c7ecb8228ae35d2bb743f4f to your computer and use it in GitHub Desktop.
WordPress | Content for your posts page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // If your title is within the header or another include | |
| if ( is_home() ) { | |
| $postspage = get_option('page_for_posts'); | |
| $postspagetitle = get_the_title( $postspage ); | |
| echo '<h1>' . $postspagetitle . '</h1>'; | |
| } | |
| ?> | |
| <?php | |
| // For your themes index.php file | |
| $postspage = get_option('page_for_posts'); // Returns the ID | |
| $thepage = get_post( $postspage ); | |
| $content = $thepage->post_content; // Get raw page content | |
| $postspagetitle = get_the_title( $postspage ); | |
| echo '<h1>' . $postspagetitle . '</h1>'; | |
| if ( !empty( $content ) ) { | |
| $content = apply_filters('the_content', $content); // Apply filters to add formating | |
| echo '<div class="the-content">' . $content . '</div>'; | |
| if ( current_user_can( 'edit_pages' ) ) { | |
| // Let us also add an edit link for ease during development | |
| $editlink = get_edit_post_link( $postspage ); | |
| echo '<a href="' . $editlink . '" class="edit">Edit</a>'; | |
| } | |
| } | |
| ?> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Posts page content
I've always found it frustrating that once you specify the page in; Settings > Reading > Posts Page, any content in that page vanishes.
This is because WordPress uses your themes index.php file for the posts page template. It is, generally, a fairly slim file that simply outputs the posts.
However, we often want our posts page to contain some sort of introduction, and we want the title to be the title of the posts page itself as opposed to the title of the latest post.
The above code fixes this. It grabs the title and the content from the page you've set as the posts page within settings.
Also note, even outside of index.php we can check to see if we are on the posts page by using is_home(), hence the first chunk of code which could go in your header file or another include.