最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Create category after theme setup and modify the default one

programmeradmin1浏览0评论

I need to modify the default category that is created by wordpress after the installation. I want to create a new one and modify the existing one after that my theme is activated. Is this possible?

I need to modify the default category that is created by wordpress after the installation. I want to create a new one and modify the existing one after that my theme is activated. Is this possible?

Share Improve this question edited Feb 15, 2020 at 1:58 RiddleMeThis 3,8078 gold badges22 silver badges30 bronze badges asked Feb 14, 2020 at 16:30 sialfasialfa 32910 silver badges29 bronze badges 2
  • yes, what have you got so far? – RiddleMeThis Commented Feb 14, 2020 at 16:35
  • @RiddleMeThis nothing because I don't know if there is an hook or similar to achieve this, this is why I'm asking here. – sialfa Commented Feb 14, 2020 at 19:03
Add a comment  | 

1 Answer 1

Reset to default 0

You can use wp_update_term to modify terms (even the default uncategorized) and wp_insert_term to update existing terms.

Here is a basic example that should get you there.

function add_category(){

    // Update Uncategorized Category (1)
    wp_update_term(
        1, 
        'category',
        array(
          'name' => 'New Category Name',
          'slug' => 'new-category-slug'
        )
    );

    // Insert New Category
    if(!term_exists('another-category')) {
        wp_insert_term(
            'Another Category',
            'category',
            array(
                'slug' => 'another-category'
            )
        );
    }
}

add_action('after_setup_theme', 'add_category');

This is tested and works.

发布评论

评论列表(0)

  1. 暂无评论