Forums

[resolved] Automatic Category Creation (6 posts)

  1. nospario
    Member
    Posted 1 year ago #

    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.

  2. nospario
    Member
    Posted 1 year ago #

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

  3. nospario
    Member
    Posted 1 year ago #

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

  4. nospario
    Member
    Posted 1 year ago #

    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');
    ?>
  5. nospario
    Member
    Posted 1 year ago #

    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.

  6. nospario
    Member
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic