separate_comments

函式
separate_comments ( $comments )
引數
  • (WP_Comment[]) $comments Array of comments
    Required:
返回值
  • (WP_Comment[]) Array of comments keyed by comment_type.
定義位置
相關方法
get_commentshave_commentsthe_commentwp_update_commentwp_insert_comment
引入
2.7.0
棄用
-

separate_comments: 這是一個WordPress的函式,它根據評論的型別將其分離成不同的陣列。它通常被用來分別顯示評論和回帖,並定製每種型別的輸出: 這個函式有一個引數,就是要分離的評論陣列。

將一個評論陣列分離成一個以comment_type為關鍵字的陣列。

function separate_comments( &$comments ) {
	$comments_by_type = array(
		'comment'   => array(),
		'trackback' => array(),
		'pingback'  => array(),
		'pings'     => array(),
	);

	$count = count( $comments );

	for ( $i = 0; $i < $count; $i++ ) {
		$type = $comments[ $i ]->comment_type;

		if ( empty( $type ) ) {
			$type = 'comment';
		}

		$comments_by_type[ $type ][] = &$comments[ $i ];

		if ( 'trackback' === $type || 'pingback' === $type ) {
			$comments_by_type['pings'][] = &$comments[ $i ];
		}
	}

	return $comments_by_type;
}

常見問題

FAQs
檢視更多 >