0

原谅我的php,我对它很陌生。

我在 Wordpress 中创建了一个类别循环,以根据类别输出不同的标记(因此类别 1 的元素将获得一个唯一的类等)。我有它,所以它与用户可以选择的单选按钮相呼应。一切正常,除了单选按钮不检查。我可以取消选中它们,但我无法选中它们。静态 html 版本工作正常。

静态(工作)版本:

<input id="type-1" name="s1" type="radio" class="type-1">
<label for="type-1" class="label-1">&nbsp;2010 </label>

<input id="type-3" name="s1" type="radio" class="type-3">
<label for="type-3" class="label-3">&nbsp;2011 <span>.</span> </label>

<input id="type-4" name="s1" type="radio" class="type-4">
<label for="type-4" class="label-4">&nbsp;2012 <span>.</span> </label>

<input id="type-5" name="s1" type="radio" class="type-5">
<label for="type-5" class="label-5">&nbsp;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}\">&nbsp;{$category->name}<span>.</span> </label>";
            foreach($posts as $post) {
              setup_postdata($post); ?>
              <?php
            } // foreach($posts
          } // if ($posts
        } // foreach($categories
    ?>

更新:想通了。我是个白痴,它与上面的代码无关。这是一个愚蠢的 CSS 问题。

4

1 回答 1

0

你的输出中缺少花括号,你应该这样写:

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

于 2013-09-07T02:59:54.850 回答