Comments on the ‘Posts Page’ in WordPress

If you are using a static home page and have set one of your other pages to be the ‘posts page‘ [Codex], then there is no direct way to allow comments on that page.

You will need to do some custom programming to allow viewers to comment on the ‘posts page‘, i.e. not just on the individual posts, but on the whole page in general.

WordPress does not use any page templates for the ‘posts page‘, but uses index.php or home.php of the currently active theme – see template hierarchy.

This example assumes that the theme does not have a home.php, so we are doing the coding in index.php.

First, we need to check that we are on the ‘posts page‘, and that is set under dashboard – settings – reading:

if( is_home() && get_option('page_for_posts') ) :

Then we get the ‘original’ page using WP_Query():

$posts_page = new WP_Query( array( 'post_type' => 'page', 'posts_per_page' => 1, 'post__in' => array(get_option('page_for_posts')) ) );

We could then for instance output the content of that page as it is written in the page editor; this content would otherwise not get shown for a ‘posts page’; or use ‘get_template_part(‘content’,’page’)’ as in the example for Twenty Twelve.

Last we output the comments and comment form with:

global $withcomments; $withcomments = 1;
comments_template();
$withcomments = 0; 

The $withcomments = 1;  is needed to show the comments part in a ‘posts page‘.

The full example code is:

<?php if( is_home() && get_option('page_for_posts') ) :
$posts_page = new WP_Query( array( 'post_type' => 'page', 'posts_per_page' => 1, 'post__in' => array(get_option('page_for_posts')) ) );
if( $posts_page->have_posts() ) while( $posts_page->have_posts() ) : $posts_page->the_post();
get_template_part('content','page');
//this is for Twenty Twelve; or whatever you want to output from the 'posts page'
global $withcomments; $withcomments = 1;
comments_template();
$withcomments = 0;
endwhile; wp_reset_postdata();
endif; ?>

Leave a Reply

Your email address will not be published. Required fields are marked *