Add HTML To End of WordPress Post Content

June 21, 2016

I leave comments open for most if not all the posts on this site and I have features such as Social Share, Related Posts under the main post content and before where the comments section starts.

I wanted to link directly to the comments section from under the main post content, and specifically to the comment reply form text area.

As I couldn’t find any code online to do exactly what I wanted, I wrote some custom code.

The code displays one of 3 HTML snippets depending in whether there are no comments, 1 comment or more than one comment.

Copy and paste it into your template’s functions.php file, and edit it for your site’s custom requirements.

I hope you find it useful.

add_action('the_content', 'under_content_comment_function');

function under_content_comment_function($content)
{
	if (is_single() && comments_open() )
	{
		if ( !get_comments_number() )
		{
			$content .= '<h3 class="under-post-comment"><a href="#reply-title">Click here to Post your comment, question, share, or answer related to the above post.</a></h3><br>';
		}
		elseif ( get_comments_number()==1 )
		{
			$content .= '<h3 class="under-post-comment">There is 1 comment to the above post.<br><a href="#reply-title"></h3><h3>Click here to Post your comment, question, share, or answer.</a></h3><br>';
		}
		else
		{
			$content .= '<h3 class="under-post-comment">There are '.get_comments_number().' comments to the above post.</h3><h3><a href="#reply-title">Click here to Post your comment, question, share, or answer.</a></h3><br>';
		}
	}
	return $content;
}

 

Share this:
About Bharat Karavadra

"I research and share leading-edge information, tools and exercises to help people transform and heal their life situations."

Share your thoughts

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

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
Bharat Karavadra