我使用此代码检查Wordpress类别中使用的标签。它呈现一个复选框列表。我希望表单在提交后能够记住我的选择。
这是我的代码:
<form name="tags" onChange="document.forms.tags.submit();">
<?php
if (is_category()){
$cat = get_query_var('cat');
$yourcat = get_category ($cat);
}
$tag_IDs = array();
query_posts('category_name='.$yourcat->slug);
if (have_posts()) : while (have_posts()) : the_post();
$posttags = get_the_tags();
if ($posttags):
foreach($posttags as $tag) {
if (!in_array($tag->term_id , $tag_IDs)):
$tag_IDs[] = $tag->term_id;
$tag_names[$tag->term_id] = $tag->name;
$tag_slug[$tag->term_id] = $tag->slug;
endif;
}
endif;
endwhile; endif;
wp_reset_query();
if (!empty($tag_IDs)){
echo '<h3>Het meest geschikt voor</h3>';
echo "<input type=\"radio\" name=\"tag\" value=\"\"> Alles weergeven<br>";
}
foreach($tag_IDs as $tag_ID){
echo '<input type="radio" name="tag" value="'.$tag_slug[$tag_ID].'"> '.$tag_names[$tag_ID].'<br>';
}
?>
</form>
在我使用此代码记住选择之前,它不再适用于上述代码:
if((isset($_GET["tag"])) && $_GET["tag"] == "tag-example") { echo "checked";}
我认为它会像这样工作,但它不会:
echo '<input type="radio" name="tag" value="'.$tag_slug[$tag_ID].'" '.if((isset($_GET["tag"])) && $_GET["tag"] == "oudere-kinderen") { echo "checked";}.'> '.$tag_names[$tag_ID].'<br>';
如何让表单记住复选框选项?
在@Jouke的帮助下,我找到了这个解决方案:
if (is_category()){
$cat = get_query_var('cat');
$yourcat = get_category ($cat);
}
$tag_IDs = array();
query_posts('category_name='.$yourcat->slug);
if (have_posts()) : while (have_posts()) : the_post();
$posttags = get_the_tags();
if ($posttags):
foreach($posttags as $tag) {
if (!in_array($tag->term_id , $tag_IDs)):
$tag_IDs[] = $tag->term_id;
$tag_names[$tag->term_id] = $tag->name;
$tag_slug[$tag->term_id] = $tag->slug;
endif;
}
endif;
endwhile; endif;
wp_reset_query();
if (!empty($tag_IDs)){
echo '<h3>Het meest geschikt voor</h3>';
echo "<input type=\"radio\" name=\"tag\" value=\"\"> Alles weergeven<br>";
}
foreach($tag_IDs as $tag_ID){
$test = $tag_slug[$tag_ID];
echo '<input type="radio" name="tag" value="'.$test.'"' ;
if((isset($_GET["tag"])) && $_GET["tag"] == $test) {
echo ' checked="checked"';
}
echo '> '.$tag_names[$tag_ID].'<br>';
}
答案 0 :(得分:0)
试试这个
echo'&lt; input type =“radio”name =“tag”value =“'。$ tag_slug [$ tag_ID]。'”'。if((isset($ _ POST [“tag”]))&amp; &amp; $ _POST [“tag”] ==“oudere-kinderen”){echo“checked = checked”;}。'&gt; ”。$ tag_names加[$ TAG_ID]。 '
';
答案 1 :(得分:0)
<?php checked( $checked, $current, $echo ); ?>
示例:
<input type='radio' name='tag' value='1' <?php checked(isset($variable)); ?>>variable
另一个例子(php):
$test=test;
echo "<input type='radio' name='tag' value='$test' checked( isset( $test ) ); >";
逃脱:
$tagslug = $tag_slug[$tag_ID];
echo '<input type="radio" name="tag" value=/"$tagslug/" checked( isset( $tagslug ) );>';
另一个没有检查wordpress功能的例子: 将其粘贴到here并测试
$test = "slug";
echo '<input type="radio" name="tag" value="$test"' ;
if (isset($test)) {
echo 'checked="checked"';
}
echo '>';