我试图制作一个简单的名字列表,链接到个人资料信息。列表中的一些人有多个地址,因此会多次出现在列表中(因为他们的地址会出现在个人资料页面上)。我想删除重复的名称。我尝试使用array\\u unique(),但似乎没有任何效果,重复项仍然显示出来。也许我用的地方不对?我会很感激你的帮助。注意:使用ACF Pro,因此使用了“the\\u field()”和“get\\u field()”。这是我当前的代码:
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<div id="ipn-in-office-section" class="et_pb_section et_pb_section_1 et_section_regular et_section_transparent">
<div class="et_pb_row et_pb_row_1 ipn-by-county-row">
<div class="et_pb_column et_pb_column_3_4 et_pb_column_2 et_pb_css_mix_blend_mode_passthrough et-last-child">
<div class="et_pb_module et_pb_text et_pb_text_2 et_pb_bg_layout_light et_pb_text_align_left">
<?php
$io_name = $_GET[\'io_name\'];
$posts = get_posts(array(
\'posts_per_page\' => -1,
\'post_type\' => \'page\',
\'meta_key\' => \'io_name\',
\'orderby\' => \'meta_value\',
\'order\' => \'ASC\'
));
if( $posts ):
foreach( array_unique($posts, SORT_REGULAR) as $post ):
setup_postdata( $post );
?>
<?php if (get_field(\'io_name\')) { ?>
<div class="ipn-in-office-listing">
<p>
<a href="/in-office-services/by-provider-name?io_name=<?php the_field(\'io_name\'); ?>" target="_parent"><strong style="color: #1068a5;"><?php the_field(\'io_name\'); ?></strong></a> .
</p>
</div>
<?php } ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php endwhile; ?>
</div> <!-- .et_pb_text -->
</div> <!-- .et_pb_column -->
</div> <!-- .et_pb_row -->
</div>
SO网友:Mark
我之所以能够回答我的问题,是因为:https://wpquestions.com/Remove_duplicates_from_ACF_query/12685
<?php
$posts = get_posts(array(
\'numberposts\' => -1,
\'post_type\' => \'page\',
\'orderby\' => \'meta_value\',
\'order\' => \'ASC\',
));
if($posts)
{ /* FIRST, get all the post data */
foreach($posts as $post)
{ /* SECOND, one-by-one, add each data row for the specific field into an array */
$io_names[] = get_field(\'io_name\');
} /* THIRD, run through the array and remove all duplicates */
$io_names = array_unique($io_names);
foreach($io_names as $io_name)
{ /* FINALLY, display the data on the page */
echo \'<p><a href="#">\' . $io_name . \'</a></p>\';
}
}
?>