Add categories to pages

Home » Snippets » Add categories to pages
0

Created with:

Visibility: 

public

Creator: paulgee98@gmail.com

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php
/**
 * Plugin Name: Your Plugin Name
 * Description: Add Categories to Pages
 * Version: 1.0
 * Author: Your Name
 * Author URI: Your Website
 * Text Domain: wpturbo
 */

// Add categories to pages
function wpturbo_add_categories_to_pages() {
    register_taxonomy_for_object_type('category', 'page');
}
add_action('init', 'wpturbo_add_categories_to_pages');
				

Explanation:

In the code snippet above, we create a plugin that adds categories to pages in WordPress. We use the register_taxonomy_for_object_type() function to register the 'category' taxonomy for the 'page' post type. This allows us to assign categories to pages, just like we can with posts.

We hook into the 'init' action using the add_action() function to ensure that the taxonomy registration is done early enough during the WordPress initialization process. This way, the categories will be available for pages when the site loads.

Make sure to replace 'Your Plugin Name', 'Your Name', and 'Your Website' with your own plugin information. Also, don't forget to use the 'wpturbo' text domain for translation purposes.

Please note that adding categories to pages might not be the best practice in terms of WordPress hierarchy. Pages are usually organized using parent-child relationships and custom post types. However, if you have a specific requirement to add categories to pages, this code snippet will help you achieve that.

Register an account to save your snippets or go Pro to get more features.