是否显示在插件中选中的已保存类别?

时间:2015-03-24 作者:Lucky

我试图只显示已选中的类别,其他类别未选中。我正在使用循环,但它不能正常工作。我创建如下代码:

     <tr>   
    <th><label><?php _e("Category:"); ?></label></th>
    <td>
    <?php 
$args = array(
                \'child_of\'                 => get_cat_ID(single_cat_title( \'\', false )),
                \'orderby\'                  => \'name\',
                \'order\'                    => \'ASC\',
                \'hide_empty\'               => 1,
                \'hierarchical\'             => 1,
                \'exclude\'                  => \'\',
                \'include\'                  => \'\',
                \'number\'                   => \'\',
                \'taxonomy\'                 => \'category\',
                \'pad_counts\'               => false 

            ); 

     ?>
   <?php $categories = get_categories( $args ); ?>

    <?php
     $category_explode = get_the_author_meta( \'Category\', $user->ID );
     //output 4,5,16 
      $cat_ex = explode(\',\',$category_explode); 
      foreach ($cat_ex as $cat_expld)   {

     foreach ($categories as $cat)  {

         ?>
     <label> <input type="checkbox" name="category[]" <?php if($cat_expld==$cat->cat_ID){  echo "checked=\'checked\'"; } ?> value="<?php echo $cat->cat_ID; ?>" ><?php echo $cat->cat_name; ?> 
     </label>
        <?php   } }  ?>  
         </td>
         </tr>

1 个回复
SO网友:Lucky

我解决了自己的问题如下:

foreach ($categories as $cat)   {
$category_explode = get_the_author_meta( \'Category\', $user->ID );
//output 4,5,16 
$cat_ex = explode(\',\',$category_explode); 
?>
<label> <input type="checkbox" name="category[]" value="<?php echo $cat->cat_ID; ?>" <?php foreach ($cat_ex as $cat_expld)  { if($cat->cat_ID == $cat_expld){echo\'checked="checked"\';} }?> /><span> <?php echo $cat->cat_name; ?>

  </label>
     <?php  }    ?>

结束

相关推荐

如何获取wp_Dropdown_Categories的值

我正在制作一个小部件,我使用了wp_dropdown_categories() 功能。我搞不懂的是如何从下拉列表中获取所选值。所选变量存储在哪个变量中?$args = array(\'show_option_none\' => \'No Category\',\'hide_empty\' => 0); wp_dropdown_categories( $args );