T
T
tbalero2015-10-16 12:33:33
WordPress
tbalero, 2015-10-16 12:33:33

How to display a link to the parent comment in tree-like comments in a specific child comment?

On a WordPress site, to display comments on post pages of an arbitrary "movies" type:
- in the "single-movies.php" template (for a separate post page of the "movies" type) a function is used - a custom file comments.php has been created with the following content:<?php comments_template(); ?>

<?php if (comments_open()) { ?>
    <?php if (get_comments_number() == 0) { ?>
    <?php } else { ?>
    <ul class="commentlist">
      <?php
        function my_comment($comment, $args, $depth){
          $GLOBALS['comment'] = $comment; ?>
          <li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
            <div id="comment-<?php comment_ID(); ?>">

              <div class="row comment_area">

                  <div class="comment_author_ava col-xs-1">
                      <span class="glyphicon glyphicon-user" aria-hidden="true"></span>
                  </div>

                  <div class="comment_content col-xs-11">
                      <div class="comment_author_name">
                        <?php printf(__('<span>%s</span>'), get_comment_author_link()) ?>
                      </div>

                      <?php if ($comment->comment_approved == '0') : ?>
                        <em><?php _e('Ваш комментарий ожидает модерации') ?></em>
                        <br>
                      <?php endif; ?>
                      <?php comment_text() ?>
                      <div class="reply">
                        <span class="date"><?php printf(__('%1$s, %2$s'), get_comment_date(),  get_comment_time()) ?></span> <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
                      </div>
                  </div>

              </div>

            </div>
      <?php }
        $args = array(
          'reply_text' => 'Ответить',
          'callback' => 'my_comment'
        );
        wp_list_comments($args);
      ?>
    </ul>
  <?php } ?>

  <?php
    $fields = array(
      'author' => '<p class="comment-form-author"><input type="text" id="author" name="author" class="author" value="' . esc_attr($commenter['comment_author']) . '" placeholder="Введите ваше имя" pattern="[A-Za-zА-Яа-я -.]{2,}" maxlength="45" autocomplete="on" tabindex="1" required' . $aria_req . '></p>',
      'email' => '<p class="comment-form-email"><input type="email" id="email" name="email" class="email" value="' . esc_attr($commenter['comment_author_email']) . '" placeholder="Введите ваш email" maxlength="90" autocomplete="on" tabindex="2" required' . $aria_req . '></p>'
    );
 
    $args = array(
      'comment_notes_before' => '',
      'comment_notes_after' => '',
      'title_reply'  => 'Добавить комментарий',
      'comment_field' => '<p class="comment-form-comment"><textarea id="comment" name="comment" class="comment-form" cols="45" rows="4" aria-required="true" placeholder="Напишите ваш комментарий" required></textarea></p>',
      'label_submit' => 'Отправить',
      'fields' => apply_filters('comment_form_default_fields', $fields)
    );
    comment_form($args);
  ?>

  <?php } else { ?>
  <h3>Обсуждения закрыты для данной страницы</h3>
  <?php }
?>

* As a result, comments are displayed correctly
* Tree-like (nested) comments are allowed in the admin panel in the settings
Question: How can I display a link to a parent comment in tree-like comments in a specific child comment?
*To display the link to the parent comment as an anchor in the format: site.ru/post-type/nazvanie-posta#comment-72 (To scroll to the parent comment when clicking on such a link on the current page).
*The point is to then write a shift through CSS styles only for child comments no further than the second level, so that child comments of the third level, fourth level, etc. would be displayed with the same shift to the right as the second-level comments. And at the same time, in order for users to understand which parent comment a specific child comment is written to (for this, it is necessary to display a link to the parent comment in a specific child comment - as, for example, in VKontakte comments - *there this link is displayed in the form the name of the user to whom the specific comment is addressed - in the format: "11 minutes ago to Alexey ").

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya Levin, 2015-10-19
@ilyachase

Apparently, $depth in the my_comment function reflects the depth of the comment. Based on it, you can store the comment id of the previous depth, for example, using GLOBALS['last_comment_id'] and $GLOBALS['depth'] defined before calling comments_template().

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question