WP_INSERT_TERM如何插入多个值作为分类术语?

时间:2014-12-31 作者:RobbertT

我有这个脚本(在线找到)可以将输入表单字段的值作为分类术语导入。我让它工作了,但现在我想导入/保存多个表单字段的值。现在,它所做的就是将字段“input\\u name”的值作为分类术语导入。但是,比方说,我也想导入“input\\u name2”和“input\\u name3”的值。我应该可以用array 和aforeach 正当我刚刚接触PHP,有人能帮我吗?下面是代码(请注意replace函数将空格替换为-)。非常感谢!

//Custom field to custom taxonomy script
$parent_term2 = term_exists( \'the_tax\' ); // array is returned if taxonomy is given
$parent_term_id2 = $parent_term2[\'term_id\']; // get numeric term id
$plaats2 = $_POST[\'input_name\'];
$plaatsn2 = ucwords(strtolower( $plaats2 ));
$plaatsn3 = array(" ");
$plaatsn4   = array("-");

$plaatsn5 = str_replace($plaatsn3, $plaatsn4, $plaatsn2);

$cid2 = wp_insert_term(
$plaatsn5, // the term 
  \'the_tax\', // the taxonomy
  array(
    \'description\'=> \'Tax term \'.$plaatsn5.\'!\',
    \'slug\' => $plaatsn5, 
    \'parent\'=> $parent_term2[\'term_id\']
  )
);

wp_set_object_terms( $listing_id, $plaatsn5, \'the_tax\', false);

2 个回复
最合适的回答,由SO网友:cybmeta 整理而成

我会将术语的输入字段构建为输入数组。像这样(注意[] 在名称属性中:

<input name="input_name[]" type="text" value="">
<input name="input_name[]" type="text" value="">
<input name="input_name[]" type="text" value="">
然后,在PHP中:

//Now $_POST[\'input_name\']; is an array
//get the array and sanitize it
$input_terms = array_map( \'sanitize_text_field\', $_POST[\'input_name\'] );
//Set the array of terms for later use on wp_set_object_terms
$terms = array();
foreach( $input_terms as $term ) {

    $existent_term = term_exists( $term, \'the_tax\' );

    if( $existent_term && isset($existent_term[\'term_id\']) ) {

        $term_id = $existent_term[\'term_id\'];

    } else {

        //intert the term if it doesn\'t exsit
        $term = wp_insert_term(
            $term, // the term 
            \'the_tax\' // the taxonomy
        );
        if( !is_wp_error($term ) && isset($term[\'term_id\']) ) {
             $term_id = $term[\'term_id\'];
        } 

   }

   //Fill the array of terms for later use on wp_set_object_terms
   $terms[] = (int) $term_id;

}

//I assume that $listing_id is correct, you didn\'t shou it in your code
wp_set_object_terms( $listing_id, $terms, \'the_tax\' );

UPDATE

我认为这不是一个很好的方法,但正如您所问:

<input name="input_name1" type="text" value="">
<input name="input_name2" type="text" value="">
<input name="input_name3" type="text" value="">
然后,在PHP中:

//Populate an array with the input fields
$input_terms = array();
$input_terms[] = sanitize_text_field( $_POST[\'input_name1\'] );
$input_terms[] = sanitize_text_field( $_POST[\'input_name2\'] );
$input_terms[] = sanitize_text_field( $_POST[\'input_name3\'] );

//Set the array of terms for later use on wp_set_object_terms
$terms = array();
foreach( $input_terms as $term ) {

    $existent_term = term_exists( $term, \'the_tax\' );

    if( $existent_term && isset($existent_term[\'term_id\']) ) {

        $term_id = $existent_term[\'term_id\'];

    } else {

        //intert the term if it doesn\'t exsit
        $term = wp_insert_term(
            $term, // the term 
            \'the_tax\' // the taxonomy
        );
        if( !is_wp_error($term ) && isset($term[\'term_id\']) ) {
             $term_id = $term[\'term_id\'];
        } 

   }

   //Fill the array of terms for later use on wp_set_object_terms
   $terms[] = (int) $term_id;

}

//I assume that $listing_id is correct, you didn\'t shou it in your code
wp_set_object_terms( $listing_id, $terms, \'the_tax\' );
注意:您不需要str\\u replace函数来替换“-”的空格。wp_insert_term 从术语名称生成slug。

SO网友:Rahul Jalavadiya

您可以使用foreach循环来完成。。例如,请检查以下示例

private function set_post_terms( $post, $value, $taxonomy ) {

/* First check to see if there are multiple terms and,
 * if so, then loop through the values and update each
 * term. 
 */
if ( $this->has_multiple_terms( $value ) ) {
    $this->set_multiple_terms( $post, $value, $taxonomy );
} else {

    $term = term_exists( strtolower( $value ), $taxonomy );

    // If the taxonomy doesn\'t exist, then we create it
    if ( 0 === $term || null === $term ) {

        $term = wp_insert_term(
            $value,
            $taxonomy,
            array(
                \'slug\' => strtolower( str_ireplace( \' \', \'-\', $value ) )
            )
        );

    }

}

    // Then we can set the taxonomy
    wp_set_post_terms( $post[\'ID\'], $term, $taxonomy, true );

}

结束