我假设您的原始代码末尾有一个结尾},如下所示:
<?php
global $post;
$postslist = get_posts( array( \'post_type\' => \'reviews\', \'posts_per_page\' => 20, \'order\'=> \'ASC\', \'orderby\' => \'date\' ) );
foreach ( $postslist as $post ){
setup_postdata($post);
// Tag counter
$post_tags = get_the_tags($post->ID);
if ($post_tags) {
foreach($post_tags as $tag){
echo \'<span class="review-count">\' . $tag->count . \' review(s)\' . \'</span>\';
}
}
}
?>
您需要设置一个数组来保存您的
foreach
循环,使其持续。
<?php
global $post;
$postslist = get_posts( array( \'post_type\' => \'reviews\', \'posts_per_page\' => 20, \'order\'=> \'ASC\', \'orderby\' => \'date\' ) );
// Set up an empty array
$review_titles = array();
foreach ( $postslist as $post ) {
setup_postdata($post);
// Use the title rather than tags
$review_title = $post->post_title;
// See if this title already exists in the array
foreach($review_titles as $key => $val) {
// If the title matches, add to its count
if($val[\'title\'] == $review_title) {
$count = $val[\'count\'] + 1;
$review_titles[$key][\'count\'] = $count;
// Stop looping since we already found a match
break;
}
// The title doesn\'t match, so add it to the array with count 1
else {
$review_titles[] = array(\'title\' => "$review_title", \'count\' => 1);
}
}
}
?>