我有一个非常简单的代码,列出所有自定义帖子类型,让用户使用“for each loop”选择它们
问题是当给出选定的值它是重复的,因为它已经存在于每个循环列表中它也在那里...
所以我不知道如何从下拉菜单中排除用户选择的项目任何方式希望获得一些帮助提前感谢这里的代码..
<?php // select funeral home ad's plugin
$mypostype = get_posts('post_type=funeral_homes');
if($mypostype) : ?>
<?php
$funeral_home_meta = get_post_meta($curpost->ID, '_selected_funeral_homes', true);
//echo $funeral_home_meta ;
$the_post_value = $curpost ->ID ;
//echo $the_post_value ;
$adhome_id = $funeral_home_meta;
$queried_post = get_post($adhome_id);
$ad_title = $queried_post->post_title;
?>
<label for="my_select">Funeral home ad ? </label>
<select id="my_select" name="my_select">
<?php foreach ( $mypostype as $mypost ) : ?>
<option value="<?php echo $mypost->ID ; ?>"><?php echo $mypost->post_title ?></option>
<?php endforeach; ?>
<option value="<?php echo $funeral_home_meta ?>" selected><?php echo $ad_title
//the proplem lays here ?></option>
</select>
</form>
<?php endif ?>
至于设置所选值我不得不再次提及它,它是重复的有任何替代??? ..再次感谢.. basem
答案 0 :(得分:0)
将foreach循环更改为以下内容。
<?php foreach ( $mypostype as $mypost ) :
if($mypost->ID==get_the_ID()) // check the it is current post
continue;
?>
<option value="<?php echo $mypost->ID ; ?>"><?php echo $mypost->post_title ?></option>
<?php endforeach; ?>