原谅我的php,我对它很陌生。
我在 Wordpress 中创建了一个类别循环,以根据类别输出不同的标记(因此类别 1 的元素将获得一个唯一的类,等等)。我有它,所以它回显用户可以选择的单选按钮。一切正常,除了单选按钮不检查。我可以取消选中它们,但我无法选中它们。静态 html 版本工作正常。
静态(工作)版本:
<input id="type-1" name="s1" type="radio" class="type-1">
<label for="type-1" class="label-1"> 2010 </label>
<input id="type-3" name="s1" type="radio" class="type-3">
<label for="type-3" class="label-3"> 2011 <span>.</span> </label>
<input id="type-4" name="s1" type="radio" class="type-4">
<label for="type-4" class="label-4"> 2012 <span>.</span> </label>
<input id="type-5" name="s1" type="radio" class="type-5">
<label for="type-5" class="label-5"> 2013 <span>.</span> </label>
动态(不工作)版本:
<?php
$cat_args=array(
'orderby' => 'name',
'order' => 'ASC'
);
$categories=get_categories($cat_args);
foreach($categories as $category) {
$args=array(
'category__in' => array($category->term_id),
'caller_get_posts'=>1
);
$posts=get_posts($args);
if ($posts) {
echo "<input id='"type-{$category->term_id}'" name='"s1'" type='"radio'" class='"type-{$category->term_id}'"><label for='"type-{$category->term_id}'" class='"label-{$category->term_id}'"> {$category->name}<span>.</span> </label>";
foreach($posts as $post) {
setup_postdata($post); ?>
<?php
} // foreach($posts
} // if ($posts
} // foreach($categories
?>
更新:想通了。我是个白痴,它与上面的代码无关。这是一个愚蠢的CSS问题。
你的输出中缺少大括号,你应该这样写:
echo "<input id='"type-{$category->term_id}'" ......";
查看此链接了解详细信息:
http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing
或者,您可以使用字符串串联:
echo "<input id='"type-" . $category->term_id . "'" ......";
或者你可以使用sprintf()
,我个人喜欢(http://php.net/manual/en/function.sprintf.php)