Skip to content

Fixing an issue in Sage 10 where comments don’t display

Recently I was faced with an issue while building this very blog where the post comments were not appearing beneath my posts, even though I had comments enabled and there were comments present on the post.  This theme is built using the excellent Sage 10 starter theme for WordPress, which introduces some advanced programming concepts from the Laravel world into WordPress.

As a part of the default Sage 10 theme, there is a default composer called Comments.php that is supposed to load the comments for the current post and pass them to the comments.blade.php partial.  However, even when a post had comments, no comments were appearing.  Everything in the existing code looked correct, so it was time to do some investigating.

I first started by looking at the composer.  There was a check in the responses() method for comments, using the WordPress function have_comments().  This check would always return false, even if the post did have comments that should appear.  In the documentation I noticed a key piece of information for that function:

Warning: this function will always return “false” until after comments_template() has been called. If you need to check for comments before calling comments_template() , use get_comments_number() instead.

So, in my composer I replaced have_comments() with get_comments_number() and sure enough, it returned the correct number of comments.  So, the problem must be that the Sage theme isn’t properly calling the comments_template() method.

But how should that be called in Sage, a theme which uses blade partials and the @includes directive?

Actually, it’s pretty simple.  In the file where you want to call your comments partial, in this case content-single.blade.php you just have to replace your @include directive for the comments partial with a call to comments_template(), like so:

@php
    comments_template('./comments.blade.php', false);
@endphp

Oddly enough, the fact that I’m passing a blade file in to a default WordPress function doesn’t seem to cause any issues.  Sage is likely doing some magic behind the scenes to make this work.

Hopefully this post is helpful and saved you some time.  Until next time fellow developers.

Leave a Reply

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