0

I'm creating a custom theme for my blog and I've been working on the comments for a while now. I've tried a few different strategies and read a lot of the codex and questions on here but I can't seem to get the comments to work.

I've created a custom comments.php file and placed it in the theme directory but I know it's not even getting included in the blog because I put in some console logs and they are not showing up.

It seems the problem is even more complicated than just that though, because I can get the comment form to show up, but I've tried to enter some test comments and when I click submit on the form, no comments get submitted to the database and it just redirects me to the single.php for the blog entry that I'm trying to comment on.

I've tried copying the comments.php from the 2016 theme but that made no difference either.

I've been following a few different tutorials on how to set this up and I've tried to find my answer in the wordpress codex pages that discuss comments.php but I haven't had any luck. If anyone could take a look at my blog, and let me know where I might be going wrong, I'd really appreciate the help.

I'm calling the comments section with this line of code:

<?php comments_template(); ?>

Here is the code for my custom comments.php:

<?php
if(!empty($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME'])) :
    die("You shall not pass!");
endif;

if ( post_password_required() ) {
    return;
}
?>
<script type="text/javascript">console.log("Is this thing on?");</script>
<div id="comments" class="comments-area">

    <?php if ( have_comments() ) : ?>
        <h2 class="comments-title">
            <?php
                $comments_number = get_comments_number();
                if ( 1 === $comments_number ) {
                    /* translators: %s: post title */
                    printf( _x( 'One thought on &ldquo;%s&rdquo;', 'comments title', 'twentysixteen' ), get_the_title() );
                } else {
                    printf(
                        /* translators: 1: number of comments, 2: post title */
                        _nx(
                            '%1$s thought on &ldquo;%2$s&rdquo;',
                            '%1$s thoughts on &ldquo;%2$s&rdquo;',
                            $comments_number,
                            'comments title',
                            'twentysixteen'
                        ),
                        number_format_i18n( $comments_number ),
                        get_the_title()
                    );
                }
            ?>
        </h2>

        <?php the_comments_navigation(); ?>


        <ol class="comment-list">
            <?php
                wp_list_comments( array(
                    'style'       => 'ol',
                    'short_ping'  => true,
                    'avatar_size' => 42,
                ) );
            ?>
        </ol><!-- .comment-list -->

        <?php the_comments_navigation(); ?>

    <?php endif; // Check for have_comments(). ?>

    <?php
        // If comments are closed and there are comments, let's leave a little note, shall we?
        if ( ! comments_open() && get_comments_number() && post_type_supports( get_post_type(), 'comments' ) ) :
    ?>
        <p class="no-comments"><?php _e( 'Comments are closed.', 'twentysixteen' ); ?></p>
    <?php endif; ?>

    <?php
        comment_form( array(
            'title_reply_before' => '<h2 id="reply-title" class="comment-reply-title">',
            'title_reply_after'  => '</h2>',
        ) );
    ?>

</div><!-- .comments-area -->

If any more code is needed please let me know and I will post it as soon as I can. I'd really like to get to the bottom of this. Thanks in advance!

0

Comments can be enabled on the archive page by setting the global variable $withcomments to true before calling comments_template():

global $withcomments;
$withcomments = true;
comments_template( '', true );

As Michael pointed out in a comment on the original post, comments_template() does not return output when $withcomments is true, or in a number of other scenarios:

function comments_template( $file = '/comments.php', $separate_comments = false ) {
    global $wp_query, $withcomments, $post, $wpdb, $id, $comment,
           $user_login, $user_ID, $user_identity, $overridden_cpage;

    if ( !(is_single() || is_page() || $withcomments) || empty($post) )
        return;
    ...

comment-reply JS

Normally, you'd want to include the comment-reply JS where comments are being handled (single posts), but the the built in comment-reply JS script does not seem to be intended for use on pages with multiple posts, so you'll need to roll your own solution instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.