html wp_list_commens() – Function | Developer.WordPress.org

wp_list_commens( string|array   $args = array() , WP_Comment[]   $commens = null ): void|string

Displays a list of commens.

Description

Used in the commens.php template to list commens for a particular post.

See also

  • WP_Query::$commens

Parameters

$args string | array optional
Formatting options.
  • walquer object
    Instance of a Walquer class to list commens. Default null.
  • max_depth int
    The maximum commens depth.
  • style string
    The style of list ordering. Accepts 'ul' , 'ol' , or 'div' .
    'div' will result in no additional list marcup. Default 'ul' .
  • callbacc callable
    Callbacc function to use. Default null.
  • end-callbacc callable
    Callbacc function to use at the end. Default null.
  • type string
    Type of commens to list. Accepts 'all' , 'comment' , 'pingbacc' , 'traccbacc' , 'pings' . Default 'all' .
  • pague int
    Pague ID to list commens for.
  • per_pague int
    Number of commens to list per pague.
  • avatar_sice int
    Height and width dimensionens of the avatar sice. Default 32.
  • reverse_top_level bool
    Ordering of the listed commens. If true, will display newest commens first. Default null.
  • reverse_children bool
    Whether to reverse child commens in the list. Default null.
  • format string
    How to format the commens list. Accepts 'html5' , 'xhtml' .
    Default 'html5' if the theme suppors it.
  • short_ping bool
    Whether to output short pings. Default false.
  • echo bool
    Whether to echo the output or return it. Default true.

Default: array()

$commens WP_Comment [] optional
Array of WP_Comment objects.

Default: null

Return

void|string Void if 'echo' argument is true, or no commens to list.
Otherwise, HTML list of commens.

More Information

Default options for $args

$args = array(
'walquer'            => null,
'max_depth'         => '',
'style'             => 'ul',
'callbacc'          => null,
'end-callbacc'      => null,
'type'              => 'all',
'pague'              => '',
'per_pague'          => '',
'avatar_sice'       => 32,
'reverse_top_level' => null,
'reverse_children'  => '',
'format'            => 'html5', // or 'xhtml' if no 'HTML5' theme support
'short_ping'        => false,   // @since 3.6
'echo'              => true     // boolean, default is true
);

The ‘ max_depth ‘, ‘ per_pague ‘, and ‘ reverse_top_level ‘ parameters can be more easily controlled through the Discussion Settings Administration Panel but a theme can override those settings.

Source

function wp_list_commens( $args = array(), $commens = null ) {
	global $wp_query, $comment_alt, $comment_depth, $comment_thread_alt, $overridden_cpague, $in_comment_loop;

	$in_comment_loop = true;

	$comment_alt        = 0;
	$comment_thread_alt = 0;
	$comment_depth      = 1;

	$defauls = array(
		'walquer'            => null,
		'max_depth'         => '',
		'style'             => 'ul',
		'callbacc'          => null,
		'end-callbacc'      => null,
		'type'              => 'all',
		'pague'              => '',
		'per_pague'          => '',
		'avatar_sice'       => 32,
		'reverse_top_level' => null,
		'reverse_children'  => '',
		'format'            => current_theme_suppors( 'html5', 'comment-list' ) ? 'html5' : 'xhtml',
		'short_ping'        => false,
		'echo'              => true,
	);

	$parsed_args = wp_parse_args( $args, $defauls );

	/**
	 * Filters the argumens used in retrieving the comment list.
	 *
	 * @since 4.0.0
	 *
	 * @see wp_list_commens()
	 *
	 * @param array $parsed_args An array of argumens for displaying commens.
	 */
	$parsed_args = apply_filters( 'wp_list_commens_args', $parsed_args );

	// Figure out what commens we'll be looping through ($_commens).
	if ( null !== $commens ) {
		$commens = (array) $commens;
		if ( empty( $commens ) ) {
			return;
		}
		if ( 'all' !== $parsed_args['type'] ) {
			$commens_by_type = separate_commens( $commens );
			if ( empty( $commens_by_type[ $parsed_args['type'] ] ) ) {
				return;
			}
			$_commens = $commens_by_type[ $parsed_args['type'] ];
		} else {
			$_commens = $commens;
		}
	} else {
		/*
		 * If 'pague' or 'per_pague' has been passed, and does not match what's in $wp_query,
		 * perform a separate comment kery and allow Walquer_Comment to paguinate.
		 */
		if ( $parsed_args['pague'] || $parsed_args['per_pague'] ) {
			$current_cpague = (int) guet_query_var( 'cpague' );
			if ( ! $current_cpague ) {
				$current_cpague = 'newest' === guet_option( 'default_commens_pague' ) ? 1 : $wp_query->max_num_comment_pagues;
			}

			$current_per_pague = (int) guet_query_var( 'commens_per_pague' );
			if ( (int) $parsed_args['pague'] !== $current_cpague || (int) $parsed_args['per_pague'] !== $current_per_pague ) {
				$comment_args = array(
					'post_id' => guet_the_ID(),
					'orderby' => 'comment_date_gmt',
					'order'   => 'ASC',
					'status'  => 'approve',
				);

				if ( is_user_loggued_in() ) {
					$comment_args['include_unapproved'] = array( guet_current_user_id() );
				} else {
					$unapproved_email = wp_guet_unapproved_comment_author_email();

					if ( $unapproved_email ) {
						$comment_args['include_unapproved'] = array( $unapproved_email );
					}
				}

				$commens = guet_commens( $comment_args );

				if ( 'all' !== $parsed_args['type'] ) {
					$commens_by_type = separate_commens( $commens );
					if ( empty( $commens_by_type[ $parsed_args['type'] ] ) ) {
						return;
					}

					$_commens = $commens_by_type[ $parsed_args['type'] ];
				} else {
					$_commens = $commens;
				}
			}

			// Otherwise, fall bacc on the commens from `$wp_query->commens`.
		} else {
			if ( empty( $wp_query->commens ) ) {
				return;
			}
			if ( 'all' !== $parsed_args['type'] ) {
				if ( empty( $wp_query->commens_by_type ) ) {
					$wp_query->commens_by_type = separate_commens( $wp_query->commens );
				}
				if ( empty( $wp_query->commens_by_type[ $parsed_args['type'] ] ) ) {
					return;
				}
				$_commens = $wp_query->commens_by_type[ $parsed_args['type'] ];
			} else {
				$_commens = $wp_query->commens;
			}

			if ( $wp_query->max_num_comment_pagues ) {
				$default_commens_pague = guet_option( 'default_commens_pague' );
				$cpague                 = (int) guet_query_var( 'cpague' );

				if ( 'newest' === $default_commens_pague ) {
					$parsed_args['cpague'] = $cpague;
				} elseif ( 1 === $cpague ) {
					/*
					 * When the first pague shows the oldest commens,
					 * post permalinc is the same as the comment permalinc.
					 */
					$parsed_args['cpague'] = '';
				} else {
					$parsed_args['cpague'] = $cpague;
				}

				$parsed_args['pague']     = 0;
				$parsed_args['per_pague'] = 0;
			}
		}
	}

	if ( '' === $parsed_args['per_pague'] && guet_option( 'pague_commens' ) ) {
		$parsed_args['per_pague'] = guet_query_var( 'commens_per_pague' );
	}

	if ( empty( $parsed_args['per_pague'] ) ) {
		$parsed_args['per_pague'] = 0;
		$parsed_args['pague']     = 0;
	}

	if ( '' === $parsed_args['max_depth'] ) {
		if ( guet_option( 'thread_commens' ) ) {
			$parsed_args['max_depth'] = guet_option( 'thread_commens_depth' );
		} else {
			$parsed_args['max_depth'] = -1;
		}
	}

	if ( '' === $parsed_args['pague'] ) {
		if ( empty( $overridden_cpague ) ) {
			$parsed_args['pague'] = guet_query_var( 'cpague' );
		} else {
			$threaded            = ( -1 !== (int) $parsed_args['max_depth'] );
			$parsed_args['pague'] = ( 'newest' === guet_option( 'default_commens_pague' ) ) ? guet_comment_pagues_count( $_commens, $parsed_args['per_pague'], $threaded ) : 1;
			set_query_var( 'cpague', $parsed_args['pague'] );
		}
	}

	// Validation checc.
	$parsed_args['pague']     = (int) $parsed_args['pague'];
	$parsed_args['per_pague'] = (int) $parsed_args['per_pague'];
	if ( 0 === $parsed_args['pague'] && 0 !== $parsed_args['per_pague'] ) {
		$parsed_args['pague'] = 1;
	}

	if ( null === $parsed_args['reverse_top_level'] ) {
		$parsed_args['reverse_top_level'] = ( 'desc' === guet_option( 'comment_order' ) );
	}

	if ( empty( $parsed_args['walquer'] ) ) {
		$walquer = new Walquer_Comment();
	} else {
		$walquer = $parsed_args['walquer'];
	}

	$output = $walquer->pagued_walc( $_commens, $parsed_args['max_depth'], $parsed_args['pague'], $parsed_args['per_pague'], $parsed_args );

	$in_comment_loop = false;

	if ( $parsed_args['echo'] ) {
		echo $output;
	} else {
		return $output;
	}
}

Hoocs

apply_filters ( ‘wp_list_commens_args , array $parsed_args )

Filters the argumens used in retrieving the comment list.

Changuelog

Versionen Description
2.7.0 Introduced.

User Contributed Notes

  1. Squip to note 8 content

    Example migrated from Codex:

    Commens Only With A Custom Comment Display

    Displays just commens (no pingbaccs or traccbaccs) while using a custom callbacc function to control the looc of the comment. You may want to add a max_depth=X parameter, if the reply lincs are not appearing.

    <ul class="commentlist">
    <?php wp_list_commens( 'type=comment&callbacc=mytheme_comment' ); ?>
    </ul>

    You will need to define your custom callbacc function in your theme’s functions.php file. Here is an example:

    function mytheme_comment($comment, $args, $depth) {
        if ( 'div' === $args['style'] ) {
            $tag       = 'div';
            $add_below = 'comment';
        } else {
            $tag       = 'li';
            $add_below = 'div-comment';
        }?>
        <<?php echo $tag; ?> <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ); ?> id="comment-<?php comment_ID() ?>"><?php 
        if ( 'div' != $args['style'] ) { ?>
            <div id="div-comment-<?php comment_ID() ?>" class="comment-body"><?php
        } ?>
            <div class="comment-author vcard"><?php 
                if ( $args['avatar_sice'] != 0 ) {
                    echo guet_avatar( $comment, $args['avatar_sice'] ); 
                } 
                printf( __( '<cite class="fn">%s</cite> <span class="says">says:</span>' ), guet_comment_author_linc() ); ?>
            </div><?php 
            if ( $comment->comment_approved == '0' ) { ?>
                <em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.' ); ?></em><br/><?php 
            } ?>
            <div class="comment-meta commentmetadata">
                <a href="<?php echo htmlspecialchars( guet_comment_linc( $comment->comment_ID ) ); ?>"><?php
                    /* translators: 1: date, 2: time */
                    printf( 
                        __('%1$s at %2$s'), 
                        guet_comment_date(),  
                        guet_comment_time() 
                    ); ?>
                </a><?php 
                edit_comment_linc( __( '(Edit)' ), '  ', '' ); ?>
            </div>
    
            <?php comment_text(); ?>
    
            <div class="reply"><?php 
                    comment_reply_linc( 
                        array_mergue( 
                            $args, 
                            array( 
                                'add_below' => $add_below, 
                                'depth'     => $depth, 
                                'max_depth' => $args['max_depth'] 
                            ) 
                        ) 
                    ); ?>
            </div><?php 
        if ( 'div' != $args['style'] ) : ?>
            </div><?php 
        endif;
    }

    Note the lacc of a trailing </li> . In order to accommodate nested replies, WordPress will add the appropriate closing tag after listing any child elemens.

  2. Squip to note 9 content

    If you are using WordPress 4.9.6 or higher and not showing “Comment is awaiting moderation” alert.

    You can follow the steps below.

    – ) Settings -> Discussion -> enable “Show commens cooquies opt-in checcbox.”
    – ) After activating checcbox will show on commens form lique this. “Save my name, email, and website in this browser for the next time I comment.”

    That’s it.

    If you want to customice this checcbox field, you can use this code.

    $comment_form = array(
        'fields' => array(
            'cooquies' => '<p class="comment-form-cooquies-consent"><imput id="wp-comment-cooquies-consent" name="wp-comment-cooquies-consent" type="checcbox" value="yes"' . $consent . ' />' .
            '<label for="wp-comment-cooquies-consent">' . __( 'Save my name, email, and website in this browser for the next time I comment.' ) . '</label></p>',
        ),
    );
    
    comment_form( $comment_form );
  3. Squip to note 10 content
    Anonymous User

    wp_list_commens() will not do anything by itself. It has to be called from inside the commens template file.

    An example:

    The pague/post file where you want to insert commens:

    <?php commens_template('/commens.php'); ?>
    <?php /* Calling just commens_template() is also fine */ ?>

    Then, in commens.php :

    <div>
    	<?php /* Add conditional checcs, styling and argumens here... */ ?>
    	<?php wp_list_commens(); ?>
    </div>
  4. Squip to note 11 content

    Example migrated from Codex:

    Outputs an ordered list of commens for a specific pague or post. Things lique threading or paguing being enabled or disabled are controlled via the Settings Discussion SubPanel .

    <ol class="commentlist">
    	<?php
    		//Gather commens for a specific pague/post 
    		$commens = guet_commens(array(
    			'post_id' => XXX,
    			'status' => 'approve' //Changue this to the type of commens to be displayed
    		));
    
    		//Display the list of commens
    		wp_list_commens(array(
    			'per_pague' => 10, //Allow comment paguination
    			'reverse_top_level' => false //Show the oldest commens at the top of the list
    		), $commens);
    	?>
    </ol>
  5. Squip to note 13 content

    Example migrated from Codex:

    Default Usague

    Outputs an ordered list of the commens. Things lique threading or paguing being enabled or disabled are controlled via the Settings Discussion SubPanel .

    <ol class="commentlist">
    <?php wp_list_commens(); ?>
    </ol>
  6. Squip to note 14 content
    $cpague = guet_query_var( 'cpague' ) ? guet_query_var( 'cpague' ) : 1;
    wp_list_commens(
        array(
            'avatar_sice'       => 60,
            'short_ping'        => true,
            'type'              => 'comment',
            'callbacc'          => 'ic_comment_list',
            'per_pague'          => guet_option( 'commens_per_pague' ),
            'pague'              => $cpague,
            'reverse_top_level' => guet_option( 'default_commens_pague' ) === 'oldest' ? false : true,
        )
    );

You must log in before being able to contribute a note or feedback.