【发布时间】:2014-08-11 15:33:46
【问题描述】:
我正在尝试遍历自定义帖子类型并显示元键的所有元值。
我可以显示所有值,但是有一些重复值。我想删除那些重复项。
我尝试过使用array_unique,但没有运气。
我的代码:
<?php
query_posts(array(
'post_type' => 'lejlighed'
));
?>
<?php
while (have_posts()):
the_post();
$meta = get_post_meta($post->ID, 'antal_varelser');
foreach ($meta as $m) {
echo '<option value="1">' . $m . '</option>';
}
endwhile;
?>
结果:2 2 3 4 3 3 4
我的代码array_unique:
<?php
$the_query = new WP_Query(array(
'post_type' => 'lejlighed',
));
?>
<?php
while ($the_query->have_posts()):
$the_query->the_post();
$meta = get_post_meta($post->ID, 'antal_varelser');
$result = array_unique($meta);
foreach($result as $r)
{
echo '<option value="1">' . $m . '</option>';
}
endwhile; ?>
什么都不输出。
print_r($meta) 返回:
Array ( [0] => 2 ) Array ( [0] => 12 ) Array ( [0] => 4 ) Array ( [0] => 2 ) Array ( [0] => 5 ) Array ( [0] => 2 ) Array ( [0] => 4 ) Array ( [0] => 4 ) Array ( [0] => 3 ) Array ( [0] => 3 )
有什么建议吗?
【问题讨论】:
-
你用
array_unique编码了什么?您是否尝试过使用in_array仅添加不存在的项目? -
通过查看此代码,我认为您正在查询并且有重复项。也许您的查询有误?
-
This 似乎是您的解决方案
-
查询应该是正确的,因为我需要我所有帖子的值。其中一些是重复的 - 我需要摆脱它们。