admin管理员组

文章数量:1430083

I'm trying to create a new category in the taxonomy.php called 'USSERS'. I have followed few examples online and got till this part.

function insert_term( $name,$tax,$parent='' ,$slug='') {
    $term_id = term_exists( $name, $tax);
    if ( !$term_id)
        $term_id = wp_insert_term( $name, $tax, array('parent'=>$parent,'slug'=>$ludg) );
    return $term_id;
}

insert_term('USSERS','category');

After doing the above code, i went to check the wordpress dashboard to see if I actually created the category but It doesn't show. So now i'm confused and lost because I have no idea how to proceed with the above code and make it show on the dashboard.

FUNCTIONS.PHP

<?php
if ( function_exists('register_sidebar') )
    register_sidebar();
add_filter( 'auto_update_plugin', '__return_false' );
add_filter( 'auto_update_theme', '__return_false' );
?>

function yourtheme_init() {
   insert_term('USSERS','category');
}
add_action('init', 'yourtheme_init');

function insert_term( $name,$tax,$parent='' ,$slug='') {
    $term_id = term_exists( $name, $tax);
    if ( !$term_id) {
        $term_id = wp_insert_term( $name, $tax, array('parent'=>$parent,'slug'=>$slug) );
    }
    return $term_id;
}

I'm trying to create a new category in the taxonomy.php called 'USSERS'. I have followed few examples online and got till this part.

function insert_term( $name,$tax,$parent='' ,$slug='') {
    $term_id = term_exists( $name, $tax);
    if ( !$term_id)
        $term_id = wp_insert_term( $name, $tax, array('parent'=>$parent,'slug'=>$ludg) );
    return $term_id;
}

insert_term('USSERS','category');

After doing the above code, i went to check the wordpress dashboard to see if I actually created the category but It doesn't show. So now i'm confused and lost because I have no idea how to proceed with the above code and make it show on the dashboard.

FUNCTIONS.PHP

<?php
if ( function_exists('register_sidebar') )
    register_sidebar();
add_filter( 'auto_update_plugin', '__return_false' );
add_filter( 'auto_update_theme', '__return_false' );
?>

function yourtheme_init() {
   insert_term('USSERS','category');
}
add_action('init', 'yourtheme_init');

function insert_term( $name,$tax,$parent='' ,$slug='') {
    $term_id = term_exists( $name, $tax);
    if ( !$term_id) {
        $term_id = wp_insert_term( $name, $tax, array('parent'=>$parent,'slug'=>$slug) );
    }
    return $term_id;
}
Share Improve this question edited Apr 25, 2019 at 16:15 user166752 asked Apr 25, 2019 at 15:14 user166752user166752 34 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

Where are you calling this code? You need to add it to one of the init hooks.

If I add that to either a plugin or theme init, then it adds just fine for me, with the $slug typo corrected as Mike pointed out.

echo '<p>Debug: Loaded functions.php</p>';

function yourtheme_init() {
   echo '<p>Debug: called yourtheme_init</p>';
   insert_term('USSERS','category');
}
add_action('init', 'yourtheme_init');

function insert_term($name,$tax,$parent='', $slug='') {
    echo '<p>Debug: called insert_term</p>';
    $term_id = term_exists($name, $tax);
    if (!$term_id) {
        echo '<p>Debug: adding new term</p>';
        $term_id = wp_insert_term( $name, $tax, array('parent'=>$parent,'slug'=>$slug) );
        echo '<p>Debug: new term added with ID ' . $term_id . '</p>';
    } else {
       echo '<p>Debug: term already exists with ID ' . $term_id . '</p>';
    }

    die('<p>Debug: end</p>');
    return $term_id;
}

You misspelled $slug as $ludg.

本文标签: phpcategory not display in word press grammatically