最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

theme development - How to call wp_list_comments() with callback outside the loop

programmeradmin0浏览0评论

the template function as a callback argument of wp_list_comments function, and the template function takes three args:$comment, $args, $depth, like the template function that defined in the theme twentyeleven's functions.php,

function twentyeleven_comment( $comment, $args, $depth ) {
$GLOBALS['comment'] = $comment;
switch ( $comment->comment_type ) :
    case 'pingback' :
    case 'trackback' :
?>
<li class="post pingback">
    <p><?php _e( 'Pingback:', 'twentyeleven' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( __( 'Edit', 'twentyeleven' ), '<span class="edit-link">', '</span>' ); ?></p>
<?php
     /** and more ... **/

I want to invoke this function in another place, and I have the $comment reference to a comment, but I don't know how to set the $args and $depth variables. I know the value of $depth or $args can be changed in the admin settings, so, I can't just set $depth to 5 or whatever a number.

How can I get the $args and $depth ???

Thanks in advance.

the template function as a callback argument of wp_list_comments function, and the template function takes three args:$comment, $args, $depth, like the template function that defined in the theme twentyeleven's functions.php,

function twentyeleven_comment( $comment, $args, $depth ) {
$GLOBALS['comment'] = $comment;
switch ( $comment->comment_type ) :
    case 'pingback' :
    case 'trackback' :
?>
<li class="post pingback">
    <p><?php _e( 'Pingback:', 'twentyeleven' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( __( 'Edit', 'twentyeleven' ), '<span class="edit-link">', '</span>' ); ?></p>
<?php
     /** and more ... **/

I want to invoke this function in another place, and I have the $comment reference to a comment, but I don't know how to set the $args and $depth variables. I know the value of $depth or $args can be changed in the admin settings, so, I can't just set $depth to 5 or whatever a number.

How can I get the $args and $depth ???

Thanks in advance.

Share Improve this question edited Aug 25, 2013 at 8:08 towry asked Jan 29, 2013 at 17:20 towrytowry 1311 silver badge6 bronze badges 2
  • I want to invoke the twentyeleven_comment() function, not the wp_list_comments() function. @s_ha_dum, you changed the question title? – towry Commented Jan 30, 2013 at 3:25
  • twentyeleven_comment() is a callback for wp_list_comments(). The function you want to call is wp_list_comments(). – Chip Bennett Commented Jan 30, 2013 at 3:41
Add a comment  | 

3 Answers 3

Reset to default 1

Just go ahead and call it in the same way the tventyeleven does:

<?php
                /* Loop through and list the comments. Tell wp_list_comments()
                 * to use twentyeleven_comment() to format the comments.
                 * If you want to overload this in a child theme then you can
                 * define twentyeleven_comment() and that will be used instead.
                 * See twentyeleven_comment() in twentyeleven/functions.php for more.
                 */
                wp_list_comments( array( 'callback' => 'twentyeleven_comment' ) );
            ?>

wp_list_comments function will manage $depth and other $args itself (in walker). See source code of wp_list_comments and the walker using callback arg

Well, I have the solution. first, use the global variable $comment_depth, pass it to twentyeleven_comment() function, and in the twentyeleven_comment() function, define a new variable named $defaults like this:

function twentyeleven_comment( $comment, $args, $depth ) {
     $defaults = array('walker' => null, 'max_depth' => '', 'style' => 'ul', 'callback' => null, 'end-callback' => null, 'type' => 'all','page' => '', 'per_page' => '', 'avatar_size' => 32, 'reverse_top_level' => null, 'reverse_children' => '');
     $args = wp_parse_args($args,$defaults);

 /** something else **/
}

In another function, invoke twentyeleven_comment() function like this:

 add_action('comment_post','my_action',10,2);
 function my_action($comment_id,$comment_status){
       $comment_g = &get_comment($comment_id);

       global $comment_depth;  /** here get the $depth  **/
       twentyeleven_comment($comment_g,array(),$comment_depth); /** invoke the function in this way **/

       /*** something else goes here ***/
}

I just want to output one comment by the $comment_ID

thanks to those guys who have commented or answered :)

Be sure that 'echo' is true

wp_list_comments(
            array(
                'avatar_size' => 48,
                'style'       => 'ol',
                'short_ping'  => true,
                'per_page' => get_option('comments_per_page'),
                'echo' => true,
                'callback' => 'comments_function_to_call'
            )
);
发布评论

评论列表(0)

  1. 暂无评论