使用wp_INSERT_TERM()函数添加术语时,缺少Term_id和Term_Taxonomy_id

时间:2012-04-11 作者:Average Joe

检查线路;list($term\\u id,$taxonomy\\u id)=$result在下面的代码段中。(朝向底部)。为什么我缺少术语\\u id&;当我回显$taxonomy\\u id的值时?它们不应该由wp\\u插入它们来填充吗?

$term_name = \'Uncategorized\';
$term_slug = sanitize_title(\'Uncategorized\', \'Default category slug\');
$term_taxonomy = \'category\';
$term_description = $term_slug . " desc here.. ";
$term_parentID = 0;

if ( is_term( $term_name , $term_taxonomy ) ):
    echo "this term exists!";
    die;
else:
    //echo "This terms does not exist!";
    //echo "<pre>";
    //global $wp_taxonomies;
    //print_r ($wp_taxonomies);
    //echo "</pre>";
    if (taxonomy_exists($term_taxonomy)):
        //insert the term into the wp_terms
        $args = array(
                            \'description\' => $term_description,
                            \'slug\' => $term_slug,
                            \'parent\' => $term_parentID 
                        );  

        $result = wp_insert_term($term_name, $term_taxonomy, $args);

        if ( is_wp_error( $result ) ):
            $error_string = $result->get_error_message();
            echo \'<div id="message" class="error"><p>\' . $error_string . \'</p> </div>\';
        else:
            list($term_id,$taxonomy_id) = $result;
            echo "<p>The term <i>{$term_name}</i> under the <i>{$term_taxonomy}</i> taxonomy has been added into the <i>wp_terms & wp_term_taxonomy<i> tables. The Term ID is {$term_id} and the related taxonomy ID is {$term_taxonomy_id}.";
            //outputs as The term Uncategorized under the category taxonomy has been added into the wp_terms & wp_term_taxonomy tables. The Term ID is and the related taxonomy ID is .
        endif;                          
    else:
        echo "<p>{$term_taxonomy} does not exist. we cannot add this $term. Please use the register_taxonomy function to register this taxonomy. ";
    endif;
endif;

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

问题是php函数的使用list. 如果成功,wp_insert_term 返回以下形式的内容:

array(2) { ["term_id"]=> int(307) ["term_taxonomy_id"]=> int(325) }
您不能使用list 将此作为list 仅适用于数字阵列。相反,请尝试extract 而是:

extract($result);
echo "<p>The term <i>{$term_name}</i> under the <i>{$term_taxonomy}</i> taxonomy has been added into the <i>wp_terms & wp_term_taxonomy<i> tables. The Term ID is {$term_id} and the related taxonomy ID is {$term_taxonomy_id}.";

SO网友:Average Joe
//------get term/taxonomy data

$term_name = \'Uncategorized\';
$term_slug = \'Default category slug\';  //this gets sanitized by the blp_wordpress_insert_term function!
$term_taxonomy = \'category\';
$term_description = $term_name . " desc here.. ";
$term_parentID = 0;

//------byref stuff: library function will populate them in return

$term_id = null; 
$term_taxonomy_id = null; 
$term_error = null; 

//------call the library function

if (blp_wordpress_insert_term($term_name,$term_slug,$term_taxonomy,$term_description,$term_parentID,$term_id,$term_taxonomy_id,$term_error)):
    echo "<p>" , "The term <i>{$term_name}</i> under the <i>{$term_taxonomy}</i> taxonomy has been added into the <i>wp_terms & wp_term_taxonomy<i> tables. The Term ID is {$term_id} and the related taxonomy ID is {$term_taxonomy_id}.";
else:
    echo "<p>" , $term_error;
    die;
endif;

//-----------------------------here is the library function

function blp_wordpress_insert_term($term_name,$term_slug,$term_taxonomy,$term_description,$term_parentID,&$term_id,&$term_taxonomy_id,&$term_error){

    /*

    inserts a term under the given taxonomy and returns the term_id and term_taxonomy_id byref if everything is cool/

    it also returns as TRUE or FALSE as a whole depending on a proper entry

    it also populates the $term_error variable - passed byref if an error occurs

    TO DO: validate the passed parentid to see if its a valid ID 

    */

    $term_slug = sanitize_title($term_name, $term_slug);

    if ( term_exists( $term_name , $term_taxonomy ) ):
        $term_error = "ERROR: The term ({$term_name}) already exists!";
        return false;

    else:

        if (taxonomy_exists($term_taxonomy)): //insert the term into the wp_terms

            $args = array(
                                \'description\' => $term_description,
                                \'slug\' => $term_slug,
                                \'parent\' => $term_parentID 
                            );  

            $result = wp_insert_term($term_name, $term_taxonomy, $args);
            //print_r ($result); // Array ( [term_id] => 1 [term_taxonomy_id] => 1 )

            if ( is_wp_error( $result ) ):
                $term_error = "ERROR: " . $result->get_error_message();
                return false;
            else:
                $term_id = $result[\'term_id\'];
                $term_taxonomy_id = $result[\'term_taxonomy_id\']; 
                return true;
            endif;                          
        else:
            $term_error = "ERROR: The taxonomy {$term_taxonomy} does not exist. blp_wordpress_insert_term cannot add a term when its associated taxonomy missing. Use the <i>register_taxonomy</i> function to register the taxonomy <i>{$term_taxonomy}. ";
            return false;
        endif;
    endif;

}

结束

相关推荐

Taxonomy Checkbox Admin Panel

我有一个基本的管理面板,允许管理员使用复选框保存选项。复选框的使用是因为该选项需要多选因此,管理选项-复选框1-复选框2-复选框3等我的复选框是动态生成的,使用<input type=\"checkbox\" name=\"firm\" id=\"firm-<?php echo esc_attr( $term->slug ); ?>\" value=\"<?php echo esc_attr( $term->slug ); ?>\" <?php checke