Add HTML To End of WordPress Post Content

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:

Subscribe for updates

Enter your email address below. I’ll send you updates & news to your email address. You can unsubscribe at any time.


Posted

in

by

Tags:


Comments

Leave a Reply

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

>