将Metabox复选框值保存为自定义内容类型

时间:2017-02-15 作者:Tiyome

我正在尝试将复选框值保存到自定义内容类型的类别中。我已经打印了ID的值,带有复选框值的数组和所有看起来都很好的值,但最后,这些值没有保存。

我的自定义内容类型是“portfolio”,他的类别是“portfolio\\u categories”。

这是我的代码:

add_action( \'admin_menu\', \'enhanced_portfolio_categories_remove_meta_box\');
function enhanced_portfolio_categories_remove_meta_box(){
   remove_meta_box(\'portfolio_categoriesdiv\', \'portfolio\', \'normal\');
}

//Add new taxonomy meta box
 add_action( \'add_meta_boxes\', \'enhanced_portfolio_categories_add_meta_box\');
 function enhanced_portfolio_categories_add_meta_box() {
     add_meta_box( \'portfolio_categoriesdiv\', \'My Custom Portfolio Categories\',\'enhanced_portfolio_categories_category_metabox\',\'portfolio\' ,\'side\',\'high\');
 }

  function enhanced_portfolio_categories_category_metabox( $post ) {

      //Set up the taxonomy object and get terms
$taxonomy = \'portfolio_categories\';
$tax = get_taxonomy($taxonomy);//This is the taxonomy object

//The name of the form
$name = \'tax_input[\' . $taxonomy . \']\';

//Get all the terms for this taxonomy
$terms = get_terms($taxonomy,array(\'hide_empty\' => 0));
     echo \'<div id="\' . $taxonomy . \'-all" class="tabs-panel">\';
     echo \'<ul id="\' . $taxonomy . \'-checklist" class="list-\' . $taxonomy . \'categorychecklist form-no-clear">\';

     $categories = array();

     foreach($terms as $term){
            $id = $taxonomy.\'-\'.$term->term_id;

             echo "<li id=\'$id\'><label>";

            echo "<input type=\'checkbox\' id=\'in-$id\' name=\'my_categories[]\' value=\'$term->term_id\' />$term->name<br />";

            echo "</label></li>";

        }?>

        </ul>
        </div>
        <?php



    }

//    var_dump($categories);
  //          wp_set_post_terms( $_POST[\'post_ID\'], $categories, \'category\' );


$cat_ids = array_map( \'intval\', (array)$_POST[\'my_categories\']);
$cat_ids = array_unique( $cat_ids );

//foreach ($_POST[\'my_categories\'] as $cat) {
  //  print_r(\'Category: \' .  $cat . \'<br>\');
//}
//print_r("POST ID:" . $_POST[\'post_ID\']);


wp_set_object_terms( $_POST[\'post_ID\'], $cat_ids, $taxonomy);

1 个回复
SO网友:Tiyome

我重新思考了这个问题:

最后,我用save\\u post hook解决了这个问题:

function enhanced_portfolio_categories_save_post(){

    $terminos = array();
    foreach ($_POST[\'my_categories\'] as $key) {
        $custom_tax = get_term_by(\'term_id\', $key, \'portfolio_categories\');
        array_push($terminos, $custom_tax->term_id);
    }

    wp_set_object_terms( $_POST[\'post_ID\'], $terminos, \'portfolio_categories\');

}

相关推荐

Horizontlal scrolling content

我正在寻找一个插件(或其他东西),可以复制水平子菜单和滚动内容,当你点击oeuvres/expostions/etc… :http://www.fillesducalvaire.com/artiste/helena-almeida/有没有办法得到类似的东西?首先我想使用标签,但大多数wordpress插件中标签的内容非常有限。我需要在每个子菜单中包含整个页面部分,如上面的示例所示。谢谢