如何在get_option函数中添加类别ID

时间:2018-06-27 14:49:51

标签: php wordpress

更新 我进行了一些研究,了解到我可以像在开始时一样将字段保存到数据库中的术语meta而不是选项,然后轻松输出。但是我在保存时做错了,因为术语元表中没有条目。您能看看我的代码并指出我的错误吗?

<?php 
 function realized_category_field( $term ){
 $term_id = $term->term_id;
 $term_meta = get_term_meta('realized');
?>
 <tr class="form-field">
    <th scope="row">
        <label for="term_meta[realized]"><?php echo _e('Has the project been implemented?') ?></label>
        <td>
            <select name="term_meta[realized]" id="term_meta[realized]">
                <option value="0" <?=($term_meta['realized'] === 0) ? 'selected': ''?>><?php echo _e('NO'); ?></option>
                <option value="1" <?=($term_meta['realized'] === 1) ? 'selected': ''?>><?php echo _e('YES'); ?></option>
            </select>
        </td>
    </th>
  </tr>
 <?php
  }

  // Add the dropdown to the Edit form
  add_action( 'category_edit_form_fields', 'realized_category_field' );

  // Save the field
  function realized_save_term_meta() {
     $term_id = $term->term_id;
     $old = get_term_meta($term_id, 'realized', true);
     $new = $_POST['realized'];

     if ( $new && $new !== $old ) {
       update_term_meta( $term_id, 'realized', $new );
     } elseif ( '' === $new && $old ) {
       delete_term_meta( $term_id, 'realized', $old );
     }
   }
  add_action('edited_category', 'category_edit_form_fields');

  function realized_category_columns_values( $deprecated, $column_name, $term_id) {

  if($column_name === 'realized'){
   $term_meta = get_term_meta('realized');
    if($term_meta['realized'] === 1){
     echo _e('YES');
    }else{
     echo _e('NO');
    }
  }
 }

 add_action( 'manage_category_custom_column' , 'realized_category_columns_values', 10, 3 );

// Add column to Category list

function realized_category_columns($columns)
{
  return array_merge($columns, array('realized' =>  __('Has the project been implemented?')));
}

add_filter('manage_edit-category_columns' , 'realized_category_columns');
?>

我想创建一个下拉列表来定义类别是否已关闭,然后稍后在查询中进行检查并仅打印打开的类别。

我不愿意使用此tutorial来实现代码的第一部分,但是我一直坚持输出数据并检查类别是打开还是关闭。

我的问题是我不知道如何将term_id传递给$terms,而且我不确定我是否正确地编写了if语句。

有人可以帮我吗?

 <?php
       $args = array(
           'show_option_all'    => '',
           'orderby'            => 'ID',
           'order'              => 'ASC',
       );

       $categories = get_categories($args);
       $terms = get_option('taxonomy_'.$categories['term_id']);

       echo '<ul class="realized-projects-category-list">';
         foreach($categories as $category) {
          if ($terms['taxonomy_'.$category->term_id]  === '1') {
            echo '<li>';
             echo '<a href="' . get_category_link($category->term_id) . '">' . $category->name . '</a>';
            echo '</li>';
         }
       }
       echo '</ul>';
     ?>

0 个答案:

没有答案