Php 如何在foreach循环中设置选中的单选按钮?
我正在Wordpress中创建一个自定义后端,其中一部分是能够从使用foreach循环自动更新的案例研究列表中进行选择,并在页面上插入指向所选案例研究的链接。后端中的无线电列表是使用以下代码创建的Php 如何在foreach循环中设置选中的单选按钮?,php,jquery,wordpress,foreach,radio-button,Php,Jquery,Wordpress,Foreach,Radio Button,我正在Wordpress中创建一个自定义后端,其中一部分是能够从使用foreach循环自动更新的案例研究列表中进行选择,并在页面上插入指向所选案例研究的链接。后端中的无线电列表是使用以下代码创建的 <?php $checked = ' checked="checked"'; $mypostype = get_posts(array( 'post_type' => 'case_study'
<?php
$checked = ' checked="checked"';
$mypostype = get_posts(array(
'post_type' => 'case_study',
'showposts' => -1));
?>
<?php $metabox->the_field('case-study-link'); ?>
<p id="case-study-select">
<label>Which case study goes here?</label>
<?php foreach ( $mypostype as $mypost ) { ?>
<input type="radio" name="<?php $metabox->the_name(); ?>" value="<?php echo get_permalink_by_name( $mypost->post_name ); ?>" <?php echo $checked; ?>>
<?php echo $mypost->post_title; ?>
<?php } ?>
</p>
这里是哪一个案例研究?
$i=0//设置计数器
foreach($foo作为$bar){
如果($i==0){$checked=“checked”}其他{$checked='';}
//单选按钮的东西在这里…+的东西与$bar。。。
回声“;
$i++;
}
默认情况下,将选择第一个选项,但值将根据所选单选按钮值进行更改您需要使用条件语句来设置选中的属性您希望复选框被选中的情况是什么?
$i = 0;//set counter
foreach($foo as $bar) {
if($i == 0) { $checked = "checked";}else {$checked = '';}
//radio button stuff here...+ stuff with $bar...
echo "<input type='radio' name='my_radio' value='{$bar}' {$checked}>";
$i++;
}