0

There is a function in the parent theme:

function latest_comments($list_number = 5, $cut_length = 50)
{
    global $wpdb, $output;
    $comments = $wpdb->get_results($wpdb->prepare("SELECT comment_ID, comment_post_ID, comment_author, comment_author_email, comment_date, comment_content FROM {$wpdb->comments} LEFT OUTER JOIN {$wpdb->posts} ON {$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID WHERE comment_approved = '1' AND (comment_type = '' OR comment_type = 'comment') AND user_id != '1' AND post_password = '' ORDER BY comment_date_gmt DESC LIMIT %d", $list_number));
    foreach ($comments as $comment) {
        $nickname = esc_attr($comment->comment_author) ?: __('匿名', 'kratos');
        $output .= '<a href="' . get_the_permalink($comment->comment_post_ID) . '#commentform">
            <div class="meta clearfix">
                <div class="avatar float-left">' . get_avatar($comment, 60) . '</div>
                <div class="profile d-block">
                    <span class="date">' . $nickname . ' ' . __('发布于 ', 'kratos') . timeago($comment->comment_date) . '(' . wp_date(__('m月d日', 'kratos'), strtotime($comment->comment_date)) . ')</span>
                    <span class="message d-block">' . convert_smilies(esc_attr(string_cut(strip_tags($comment->comment_content), $cut_length))) . '</span>
                </div>
            </div>
        </a>';
    }
    return $output;
}

And I want to replace it with following function in child theme(the difference is elimination of avatar float-left):

function latest_comments($list_number = 5, $cut_length = 50)
{
    global $wpdb, $output;
    $comments = $wpdb->get_results($wpdb->prepare("SELECT comment_ID, comment_post_ID, comment_author, comment_author_email, comment_date, comment_content FROM {$wpdb->comments} LEFT OUTER JOIN {$wpdb->posts} ON {$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID WHERE comment_approved = '1' AND (comment_type = '' OR comment_type = 'comment') AND user_id != '1' AND post_password = '' ORDER BY comment_date_gmt DESC LIMIT %d", $list_number));
    foreach ($comments as $comment) {
        $nickname = esc_attr($comment->comment_author) ?: __('匿名', 'kratos');
        $output .= '<a href="' . get_the_permalink($comment->comment_post_ID) . '#commentform">
            <div class="meta clearfix">
                <div class="profile d-block">
                    <span class="date">' . $nickname . ' ' . __('发布于 ', 'kratos') . timeago($comment->comment_date) . '(' . wp_date(__('m月d日', 'kratos'), strtotime($comment->comment_date)) . ')</span>
                    <span class="message d-block">' . convert_smilies(esc_attr(string_cut(strip_tags($comment->comment_content), $cut_length))) . '</span>
                </div>
            </div>
        </a>';
    }
    return $output;
}

I have search two methods: the first is pluggable function, which is not fit to me as the parent theme is the third-party. The second is specify the priority of the function. However there is no add_action code and then I can't insert add_action('after_setup_theme', 'my_child_theme_function', 20) as the tutorial do. Can anyone give other methods?

5
  • Where/how is latest_comments() actually used? Commented Jul 28, 2022 at 2:32
  • @JacobPeattie It is in the theme-widget.php.
    – Varnothing
    Commented Jul 28, 2022 at 2:58
  • But how? As part of a widget? Commented Jul 28, 2022 at 6:15
  • @JacobPeattie Yes, you can check the full code in github.com/seatonjiang/kratos
    – Varnothing
    Commented Jul 28, 2022 at 7:11
  • If it's not pluggable and it isn't a callback hook then you'd probably have to build your own version of the widget.
    – vancoder
    Commented Jul 29, 2022 at 16:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.