Taxonomy as checkboxes

时间:2013-05-23 作者:Picard102

因此,我试图让Wordpress将类别显示为自定义元框中的一系列选择框,并使用以下示例tutorial, 但当我去保存选择时,它会以预期选择ID的形式生成其他类别。它也无法保存多个选择。

    <?php


    // Add the Meta Box
    function add_custom_meta_box() {
        add_meta_box(
            \'custom_meta_box\', // $id
            \'Custom Meta Box\', // $title 
            \'show_custom_meta_box\', // $callback
            \'products\', // $page
            \'normal\', // $context
            \'high\'); // $priority
    }
    add_action(\'add_meta_boxes\', \'add_custom_meta_box\');




      // Field Array
      $prefix = \'custom_\';
      $custom_meta_fields = array(
        array(
            \'label\' => \'Category\',
            \'id\'    => \'category\',
            \'type\'  => \'tax_checkbox\'
        ) );



      // The Callback
      function show_custom_meta_box() {
      global $custom_meta_fields, $post;
      // Use nonce for verification
      echo \'<input type="hidden" name="custom_meta_box_nonce" value="\'.wp_create_nonce(basename(__FILE__)).\'" />\';

        // Begin the field table and loop
        echo \'<table class="form-table">\';
        foreach ($custom_meta_fields as $field) {
            // get value of this field if it exists for this post
            $meta = get_post_meta($post->ID, $field[\'id\'], true);
            // begin a table row with
            echo \'<tr>
                    <th><label for="\'.$field[\'id\'].\'">\'.$field[\'label\'].\'</label></th>
                    <td>\';
                    switch($field[\'type\']) {



case \'tax_checkbox\':


$terms = get_terms($field[\'id\'],\'get=all\' );
$post_terms = wp_get_object_terms( get_the_ID(), $field[\'id\'] );

$taxonomy = get_taxonomy( $field[\'id\'] );

$checked = $post_terms ? $taxonomy->hierarchical ? $post_terms[0]->term_id : $post_terms[0]->slug : null;

foreach ( $terms as $term ) {
                                $term_value = $taxonomy->hierarchical ? $term->term_id : $term->slug;
                                echo \'<input type="checkbox" value="\' . $term_value . \'" name="\' . $field[\'id\'] . \'[]" id="term-\' . $term_value . \'"\' . checked( $checked, $term_value, false ) . \' /> <label for="term-\' . $term_value . \'">\' . $term->name . \'</label><br />\';
                            }
                            echo \'<span class="description">\' . $field[\'desc\'] . \' <a href="\'.get_bloginfo( \'url\' ) . \'/wp-admin/edit-tags.php?taxonomy=\' . $field[\'id\'] . \'&post_type=\' . $page . \'">Manage \' . $taxonomy->label . \'</a></span>\';
                        break;





                    } //end switch
            echo \'</td></tr>\';
        } // end foreach
        echo \'</table>\'; // end table
      }



    // Save the Data
    function save_custom_meta($post_id) {
        global $custom_meta_fields;

        // verify nonce
        if (!wp_verify_nonce($_POST[\'custom_meta_box_nonce\'], basename(__FILE__))) 
            return $post_id;
        // check autosave
        if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE)
            return $post_id;
        // check permissions
        if (\'page\' == $_POST[\'post_type\']) {
            if (!current_user_can(\'edit_page\', $post_id))
                return $post_id;
            } elseif (!current_user_can(\'edit_post\', $post_id)) {
                return $post_id;
        }

        // loop through fields and save the data
        foreach ($custom_meta_fields as $field) {


        if($field[\'type\'] == \'tax_select\') continue;  


        if( in_array( $field[\'type\'], array( \'tax_checkboxes\' ) ) ) {
                        // save taxonomies
                        if ( isset( $_POST[$field[\'id\']] ) ) {
                            $term = $_POST[$field[\'id\']];
                            wp_set_object_terms( $post_id, $term, $field[\'id\'] );
                        }
                    }

                    else {


            $old = get_post_meta($post_id, $field[\'id\'], true);
            $new = $_POST[$field[\'id\']];
            if ($new && $new != $old) {
                update_post_meta($post_id, $field[\'id\'], $new);
            } elseif (\'\' == $new && $old) {
                delete_post_meta($post_id, $field[\'id\'], $old);
            }
        } } // end foreach

        // save taxonomies
        $post = get_post($post_id);
        $category = $_POST[\'category\'];
        wp_set_object_terms( $post_id, $category, \'category\' );
    }
    add_action(\'save_post\', \'save_custom_meta\');  



    ?>

1 个回复
SO网友:prettyboymp

您需要确保将所有项都转换为整数。传入字符串将比较id和slug,并最终创建以id为名称的新术语。

编辑具体地说,将这两行更改如下(请注意intval):

wp_set_object_terms( $post_id, intval( $term ), $field[\'id\'] );
...
wp_set_object_terms( $post_id, intval( $category ), \'category\' );

结束

相关推荐

Custom Post taxonomy template

朋友们,我需要帮助。我正在为朋友扩展Mayashop主题&;我遇到了一个问题。创建了一个名为watchdog的自定义帖子类型,为其创建了一个名为watchdog brand的分类法。。。现在是唯一的看门狗。php模板工作得很好,但我似乎找不到适合这个术语本身的模板,其中列出了它下面的所有看门狗项。已尝试存档监视程序。php,存档看门狗品牌。php但运气不好。。。有什么想法吗?我还发现它甚至没有回退到归档。php。。直接进入索引。php文件&;以下是custom\\u post的代码(&U);分