我使用Wordpress并构建了自定义帖子类型,以便从帖子中的逗号分隔列表中返回选项列表,然后以联系人表单7的形式创建一组复选框。该功能正在运行,但出于某种原因,它会将输出返回两次。我试图找出如何让它返回一次。
wpcf7_add_shortcode('cargooptions', 'createbox', true);
function createbox(){
global $post;
$model = $_GET['mz'];
$args = array(
'post_type' => 'options',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'Opts_category',
'field' => 'slug',
'terms' => strtolower($model)
),
),
);
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata($post);
$options = explode(',', get_the_content());
// output list
foreach ($options as $key => $value){
$output .= '<input type="checkbox" name="option_'.$key.'" value="'.$value.'" id="opt'.$key.'" class="optionSelect">';
$output .= '<label for="opt'.$key.'" class="span_4 colWrap"><span></span> ' . trim($value) . '</label>';
}
endforeach;
return $output;
答案 0 :(得分:1)
请参阅以下代码。
您需要在循环之前实例化$output
变量,否则它将在主循环中附加到自身。
foreach ( $myposts as $post ) : setup_postdata($post);
$options = explode(',', get_the_content());
// output list
$output = ''; // add this here so it doesn't append to itself
foreach ($options as $key => $value){
$output .= '<input type="checkbox" name="option_'.$key.'" value="'.$value.'" id="opt'.$key.'" class="optionSelect">';
$output .= '<label for="opt'.$key.'" class="span_4 colWrap"><span></span> ' . trim($value) . '</label>';
}
endforeach;
return $output;