function - Get category ID after creating a category - wordpress -
i creating 2 categories within functions.php
file when wordpress theme activated so:
/** * @desc create categories on theme activation **/ function create_my_cat () { if (file_exists (abspath.'/wp-admin/includes/taxonomy.php')) { require_once (abspath.'/wp-admin/includes/taxonomy.php'); if ( ! get_cat_id( 'work' ) ) { wp_create_category( 'work' ); } if ( ! get_cat_id( 'blog' ) ) { wp_create_category( 'blog' ); } } } add_action ( 'after_setup_theme', 'create_my_cat' );
if 2 categories work , blog not exist, create them if exist, nothing.
how id's of 2 new categories when created/if exist? need store them 2 separate variables ($work & $blog) if possible can reuse them within same file.
from wordpress manual
wp_create_category( $cat_name, $parent ):
- returns 0 on failure, category id on success.
- if category exists, not duplicated. id of original existing category returned without error.
$work = wp_create_category( 'work' ); $blog = wp_create_category( 'blog' );
i tested behavior , works written in instructions
Comments
Post a Comment