admin管理员组

文章数量:1122846

I am trying to enable nested comments in my theme. I am new to all this stuff but I am doing my best. This is the code that I have in my comments.php

<?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : // are there comments to navigate through ?>
    <nav id="comment-nav-above" class="comment-navigation" role="navigation">
        <h1 class="screen-reader-text"><?php _e( 'Comment navigation', 'triday' ); ?></h1>
        <div class="nav-previous"><?php previous_comments_link( __( '← Older Comments', 'triday' ) ); ?></div>
        <div class="nav-next"><?php next_comments_link( __( 'Newer Comments →', 'triday' ) ); ?></div>
    </nav><!-- #comment-nav-above -->
    <?php endif; // check for comment navigation ?>

    <ol class="comment-list">
        <?php
            /* Loop through and list the comments. Tell wp_list_comments()
             * to use triday_comment() to format the comments.
             * If you want to override this in a child theme, then you can
             * define triday_comment() and that will be used instead.
             * See triday_comment() in inc/template-tags.php for more.
             */
            wp_list_comments( array( 'callback' => 'triday_comment' ) );
        ?>
    </ol><!-- ment-list -->

    <?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : // are there comments to navigate through ?>
    <nav id="comment-nav-below" class="comment-navigation" role="navigation">
        <h1 class="screen-reader-text"><?php _e( 'Comment navigation', 'triday' ); ?></h1>
        <div class="nav-previous"><?php previous_comments_link( __( '← Older Comments', 'triday' ) ); ?></div>
        <div class="nav-next"><?php next_comments_link( __( 'Newer Comments →', 'triday' ) ); ?></div>
    </nav><!-- #comment-nav-below -->
    <?php endif; // check for comment navigation ?>

<!--</div>-->
<?php endif; // have_comments() ?>

This is the code that I use to display the comments

<div class="comment-author">
                                <h3><i class="fa fa-user"></i><?php print(' ')?><?php comment_author( $comment_ID ); ?></h3>
                        </div>
                                <?php if ( '0' == $comment->comment_approved ) : ?>
                                <div class="alert alert-danger">
                                    <p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'triday' ); ?></p>
                                </div>

                                <?php endif; ?>
                        <div class="comment-content">
                            <?php comment_text(); ?>
                        </div><!-- ment-content -->
                            <div class="comment-metadata">
                                <a>comment_ID ) ); ?>">
                                    <time datetime="<?php comment_time( 'c' ); ?>">
                                        <p class="text-muted"><i class="fa fa-calendar"> </i> <?php printf( _x( '%1$s at %2$s', '1: date, 2: time', 'triday' ), get_comment_date(), get_comment_time() ); ?></p>
                                    </time>
                                </a>

                                <?php edit_comment_link( __( 'Edit', 'triday' ), '<span class="edit-link">', '</span>' ); ?>
                            </div><!-- ment-metadata -->
                            <footer class="comment-meta">
                        </footer><!-- ment-meta -->
                        <?php
                            comment_reply_link( array_merge( $args, array(
                                'add_below' => 'div-comment',
                                'depth'     => $depth,
                                'max_depth' => $args['max_depth'],
                                'before'    => '<div class="reply">',
                                'after'     => '</div>',
                            ) ) );
                        ?>

I would really be glad if you could take a look at what I've done! Hannes

I am trying to enable nested comments in my theme. I am new to all this stuff but I am doing my best. This is the code that I have in my comments.php

<?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : // are there comments to navigate through ?>
    <nav id="comment-nav-above" class="comment-navigation" role="navigation">
        <h1 class="screen-reader-text"><?php _e( 'Comment navigation', 'triday' ); ?></h1>
        <div class="nav-previous"><?php previous_comments_link( __( '← Older Comments', 'triday' ) ); ?></div>
        <div class="nav-next"><?php next_comments_link( __( 'Newer Comments →', 'triday' ) ); ?></div>
    </nav><!-- #comment-nav-above -->
    <?php endif; // check for comment navigation ?>

    <ol class="comment-list">
        <?php
            /* Loop through and list the comments. Tell wp_list_comments()
             * to use triday_comment() to format the comments.
             * If you want to override this in a child theme, then you can
             * define triday_comment() and that will be used instead.
             * See triday_comment() in inc/template-tags.php for more.
             */
            wp_list_comments( array( 'callback' => 'triday_comment' ) );
        ?>
    </ol><!-- .comment-list -->

    <?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : // are there comments to navigate through ?>
    <nav id="comment-nav-below" class="comment-navigation" role="navigation">
        <h1 class="screen-reader-text"><?php _e( 'Comment navigation', 'triday' ); ?></h1>
        <div class="nav-previous"><?php previous_comments_link( __( '← Older Comments', 'triday' ) ); ?></div>
        <div class="nav-next"><?php next_comments_link( __( 'Newer Comments →', 'triday' ) ); ?></div>
    </nav><!-- #comment-nav-below -->
    <?php endif; // check for comment navigation ?>

<!--</div>-->
<?php endif; // have_comments() ?>

This is the code that I use to display the comments

<div class="comment-author">
                                <h3><i class="fa fa-user"></i><?php print(' ')?><?php comment_author( $comment_ID ); ?></h3>
                        </div>
                                <?php if ( '0' == $comment->comment_approved ) : ?>
                                <div class="alert alert-danger">
                                    <p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'triday' ); ?></p>
                                </div>

                                <?php endif; ?>
                        <div class="comment-content">
                            <?php comment_text(); ?>
                        </div><!-- .comment-content -->
                            <div class="comment-metadata">
                                <a>comment_ID ) ); ?>">
                                    <time datetime="<?php comment_time( 'c' ); ?>">
                                        <p class="text-muted"><i class="fa fa-calendar"> </i> <?php printf( _x( '%1$s at %2$s', '1: date, 2: time', 'triday' ), get_comment_date(), get_comment_time() ); ?></p>
                                    </time>
                                </a>

                                <?php edit_comment_link( __( 'Edit', 'triday' ), '<span class="edit-link">', '</span>' ); ?>
                            </div><!-- .comment-metadata -->
                            <footer class="comment-meta">
                        </footer><!-- .comment-meta -->
                        <?php
                            comment_reply_link( array_merge( $args, array(
                                'add_below' => 'div-comment',
                                'depth'     => $depth,
                                'max_depth' => $args['max_depth'],
                                'before'    => '<div class="reply">',
                                'after'     => '</div>',
                            ) ) );
                        ?>

I would really be glad if you could take a look at what I've done! Hannes

Share Improve this question asked May 3, 2014 at 8:39 HannesHHannesH 753 silver badges17 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 5

In the Settings >> Discussion Screen, there is a options tick box, 'Enable threaded (nested) comments [X] levels deep'. From there you can tick to activate threaded comments and set how many levels deep to a maximum of 10.

You should also check if your theme has the following piece of code, should normally be in the functions.php template of your theme.

wp_enqueue_script('comment-reply')

This will enqueue the script to properly display the threaded comments. I quote from the codex

Note that themes need to be specially coded to properly display threaded comments.

If not found, you can simply add the following code to your functions.php to enqueue this script. Please note, this script is included by default in the Wordpress core, so there is no need to look or add the script yourself.

function enable_threaded_comments(){
if (!is_admin()) {
     if (is_singular() && comments_open() && (get_option('thread_comments') == 1))
          wp_enqueue_script('comment-reply');
     }
}

add_action('get_header', 'enable_threaded_comments');

(Code from paulund.co.uk)

EDIT

There are a couple of errors in your code. I have tested your first set of code. There is a syntax error

Parse error: syntax error, unexpected T_ENDIF in C:\xampp\htdocs\wordpress\wp-content\themes\pietergoosen2014\comments.php on line 30

This means you have an extra endif;, so just remove that

In your comments you asked

Do you know why I get an id when I press the reply link, like this -

http://localhost:8888/testar and

http://localhost:8888/testar/?replytocom=9#respond

Yes, you forgot to make the call to comment_form();

So your code in comments.php should look like this

<?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : // are there comments to navigate through ?>
    <nav id="comment-nav-above" class="comment-navigation" role="navigation">
        <h1 class="screen-reader-text"><?php _e( 'Comment navigation', 'triday' ); ?></h1>
        <div class="nav-previous"><?php previous_comments_link( __( '← Older Comments', 'triday' ) ); ?></div>
        <div class="nav-next"><?php next_comments_link( __( 'Newer Comments →', 'triday' ) ); ?></div>
    </nav><!-- #comment-nav-above -->
    <?php endif; // check for comment navigation ?>

    <ol class="comment-list">
        <?php
            /* Loop through and list the comments. Tell wp_list_comments()
             * to use triday_comment() to format the comments.
             * If you want to override this in a child theme, then you can
             * define triday_comment() and that will be used instead.
             * See triday_comment() in inc/template-tags.php for more.
             */
             wp_list_comments( array( 'callback' => 'triday_comment' ) );
            ?>

    </ol><!-- .comment-list -->

    <?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : // are there comments to navigate through ?>
    <nav id="comment-nav-below" class="comment-navigation" role="navigation">
        <h1 class="screen-reader-text"><?php _e( 'Comment navigation', 'triday' ); ?></h1>
        <div class="nav-previous"><?php previous_comments_link( __( '← Older Comments', 'triday' ) ); ?></div>
        <div class="nav-next"><?php next_comments_link( __( 'Newer Comments →', 'triday' ) ); ?></div>
    </nav><!-- #comment-nav-below -->
    <?php endif; // check for comment navigation ?>

<!--</div>-->
<?php comment_form(); ?>

本文标签: Enable nested comments in Theme