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

Small is Beautiful – Miniature Comfy Chairs and Table

American Girl Doll tm furnitureA set of miniature comfy chairs and table for Teddy to relax into – or for your favourite American Girl ™ doll to invite a friend over for a game of cards.

miniature furniture comfy chairs and table fit for a American Girl ™ doll
Ready for your friend to join you for some time off ...

I just finished this set of small scale furniture – two Comfy Chairs and a small table –  all handcrafted from solid wood, transformed from reclaimed, no longer used furniture, upholstered and covered in pure silk.
There is also a bright pink sofa to go with it.

The size will suit an American Girl ™ doll or your best teddy baer.

Title Attribute for Post Thumbnail

two men shake hands - bronze statues in Calagary/BCThe ‘featured image’ in WordPress automatically outputs the image alt attribute, but no title attribute.

Those two attributes are used for different reasons:
– the alt attribute is used to output alternative information if the image is inaccessible, for instance if the user has opted not to show images in the site, or for screen readers for accessibility reasons;
– the title attribute would present additional information about the image, and is shown by some browsers when you hover over the image.

For a WordPress ‘featured image’, the code below is taking the image title as you see it in the media library,  to be used as the title attribute.

The influence on search engine optimisation is not clear – it seems that the alt attribute is given more importance than the title attribute.

Back to the coding – To add the image title of the featured image as the title attribute to the img tag, a little filter function is needed, added  to the  ‘post_thumbnail_html’ filter.

Instead of just adding the code to functions.php of your theme, where it would get lost when you switch themes, it is more universal to create a basic plugin:

<?php
/*
Plugin Name: Featured Image Title Attribute Output
Plugin URI: n/a
Description: adds the image title as the title attribute to the 'featured image' output.
Version: 1.0
Author: alchymyth
Author URI: http://www.transformationpowertools.com/wordpress
License: GPL2
*/

add_filter('post_thumbnail_html','add_post_thumbnail_title_attribute',10,3);

function add_post_thumbnail_title_attribute($html, $post_id, $post_thumbnail_id) {

if( $html == '' ) {
return $html;
} else {
$out = $html;
$thumbnail_image = get_posts(array('p' => $post_thumbnail_id, 'post_type' => 'attachment'));

if ($thumbnail_image && isset($thumbnail_image[0])) {

$title = $thumbnail_image[0]->post_title;
if($title) {
$output = ' title="' . esc_attr($title) . '" ';
$out = str_replace( 'class=', $output . 'class=', $html );
}
}
}
return $out;
}
?>

Upload this as a appropriately named file into the plugins folder of your WordPress installation, and activate it like any other plugin from dashboard – plugins.

Posts in Two Or Three Columns – Twenty Twelve

If your index and archive pages are getting too long, or if you want to compact the information within those pages a little bit, for instance because the featured images above each post are just too large, consider to change the template to show the posts in two columns.

This is easier done than said, with some useful css classes injected into the output of the ‘post_class()’ in the div of each post, together with some rather simple css.

For instance, to change the category archive into two columns, without editing category.php itself, add a filter function into functions.php (as always with the default themes of WordPress, this  is done within a child theme):

add_filter('post_class','category_two_column_classes');

function category_two_column_classes( $classes ) {
global $wp_query;
if( is_category() ) :
$classes[] = 'two-column-post';
if( $wp_query->current_post%2 == 0 ) $classes[] = 'two-column-post-left';
endif;
return $classes;
}

The minimum css for this is:

.two-column-post { width: 47%; float: left; margin-left: 5.9%; }
.two-column-post-left { clear: left; margin-left: 0; }

If you want to apply the same principle for instance to the posts page or other archives, replace

is_category()

with another conditional tag like

is_home()

.
The code is easily adapted to three columns (or actually any number of columns) by changing the number in the modulus operator:

add_filter('post_class','category_three_column_classes');

function category_three_column_classes( $classes ) {
global $wp_query;
if( is_category() ) :
$classes[] = 'three-column-post';
if( $wp_query->current_post%3 == 0 ) $classes[] = 'column-post-left';
endif;
return $classes;
}

the css for three columns would be:

.three-column-post { width: 30%; float: left; margin-left: 4.9%; }
.column-post-left { clear: left; margin-left: 0; }