• Resolved nospario

    (@nospario)


    Hi,

    I have created a theme which uses certain categories to display content in particular areas on pages. I know that I can export the categories into a file which can be imported when using the theme but I would prefer that these categories were created automatically when the theme is activated. Can anyone suggest the best way to do this?

    Thanks.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter nospario

    (@nospario)

    I see that I can use wp_insert_category, but how can I call this function automatically on theme activation?

    Thread Starter nospario

    (@nospario)

    Okay, l can use the functions.php file by the looks of it.

    Thread Starter nospario

    (@nospario)

    Right, I’ve got this in my functions.php file and I was expecting it to insert the category on theme activation, but it isn’t doing so. Any ideas why anyone?

    Thanks.

    <?php
    	function my_activation_settings($theme)
    	{
    	    if ( 'My Theme' == $theme )
    	    {
    			$my_cat = array('cat_name' => 'My Cat', 'category_description' => '', 'category_nicename' => 'my-cat', 'category_parent' => '');
    			$my_cat_id = wp_insert_category($my_cat);
    
    	    }
    	    return;
    	}
    
    	add_action('switch_theme', 'my_activation_settings');
    ?>
    Thread Starter nospario

    (@nospario)

    Anyone out there? I’m trying this now.

    Adding this to the functions.php file

    wp_create_category(‘My category name’);

    causes this error when I try and browse the Themes page in admin.

    The website encountered an error while retrieving http://localhost:8888/mysite/wp-admin/themes.php. It may be down for maintenance or configured incorrectly.

    I’ve managed to get posts creating fine in functions.php but categories just don’t want to play!

    Please help.

    Thread Starter nospario

    (@nospario)

    Found this, seems to do the job.

    $arg = array('description' => "my description", 'parent' => "cat_ID");
    $new_cat_id = wp_insert_term("cat name", "category", $arg);
Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Automatic Category Creation’ is closed to new replies.