category not display in word press grammatically

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;
}

Leave a Comment