TL;DR: Learn how to add categories and tags to your WordPress pages to improve organisation and SEO. This guide covers both using existing taxonomies and creating separate ones, and it explains how to implement these functionalities using the Code Snippets plugin for easy and safe customisation.
Introduction
If you’re familiar with WordPress’s categories and tags for posts, you already know how valuable they are for organising content and improving SEO. However, WordPress does not support categories and tags for pages by default. This guide will show you how to extend these functionalities to pages, making your site more manageable and SEO-friendly.
Why Add Categories and Tags to WordPress Pages?
- Better Organisation: Categorising and tagging pages help in structuring your site content logically.
- Improved SEO: Search engines can better understand your content, potentially improving your rankings.
- Enhanced User Experience: Visitors can easily navigate through related pages, increasing engagement.
How to Enable Categories and Tags on WordPress Pages
Step 1: Create a Function to Enable Categories and Tags
To add categories and tags to pages using the existing post taxonomies, you need to create a custom function. This can be done by modifying your theme’s functions.php file or using a plugin like Code Snippets.
Code to Add Categories and Tags to Pages
function add_categories_and_tags_to_pages() {
// Register categories for 'page' post type.
register_taxonomy_for_object_type('category', 'page');
// Register tags for 'page' post type.
register_taxonomy_for_object_type('post_tag', 'page');
}
// Hook into the 'init' action to execute the function.
add_action('init', 'add_categories_and_tags_to_pages');
Step 2: Add the Code Using Code Snippets
- Install and Activate Code Snippets Plugin:
- Go to your WordPress dashboard.
- Navigate to Plugins > Add New.
- Search for “Code Snippets”.
- Install and activate the Code Snippets plugin.
- Add a New Snippet:
- Go to Snippets > Add New.
- Give your snippet a descriptive title, like “Enable Categories and Tags for Pages”.
- Copy and paste the code provided above into the Code field.
- Click “Save Changes and Activate”.
This will enable categories and tags for WordPress pages using the existing category and post_tag taxonomies.
Creating a Separate Category and Tag Taxonomy for Pages
For those who prefer a separate category and tagging system for pages, you can create a new taxonomy distinct from the one used for posts.
Step 1: Create a Function to Enable Separate Categories and Tags
To add separate categories and tags for WordPress pages, you need to create custom taxonomies. This can be done by modifying your theme’s functions.php file or using a plugin like Code Snippets.
Code to Add Separate Categories and Tags to Pages
function create_page_category_taxonomy() {
// Labels for the custom category taxonomy.
$labels = array(
'name' => _x('Page Categories', 'taxonomy general name'),
'singular_name' => _x('Page Category', 'taxonomy singular name'),
'search_items' => __('Search Page Categories'),
'all_items' => __('All Page Categories'),
'parent_item' => __('Parent Page Category'),
'parent_item_colon' => __('Parent Page Category:'),
'edit_item' => __('Edit Page Category'),
'update_item' => __('Update Page Category'),
'add_new_item' => __('Add New Page Category'),
'new_item_name' => __('New Page Category Name'),
'menu_name' => __('Page Categories'),
);
// Register the custom taxonomy for categories.
register_taxonomy('page_category', 'page', array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array('slug' => 'page-category'),
));
}
function create_page_tags_taxonomy() {
// Labels for the custom tag taxonomy.
$labels = array(
'name' => _x('Page Tags', 'taxonomy general name'),
'singular_name' => _x('Page Tag', 'taxonomy singular name'),
'search_items' => __('Search Page Tags'),
'popular_items' => __('Popular Page Tags'),
'all_items' => __('All Page Tags'),
'edit_item' => __('Edit Page Tag'),
'update_item' => __('Update Page Tag'),
'add_new_item' => __('Add New Page Tag'),
'new_item_name' => __('New Page Tag Name'),
'separate_items_with_commas' => __('Separate page tags with commas'),
'add_or_remove_items' => __('Add or remove page tags'),
'choose_from_most_used' => __('Choose from the most used page tags'),
'not_found' => __('No page tags found.'),
'menu_name' => __('Page Tags'),
);
// Register the custom taxonomy for tags.
register_taxonomy('page_tag', 'page', array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array('slug' => 'page-tag'),
));
}
// Hook into the 'init' action to execute the functions.
add_action('init', 'create_page_category_taxonomy');
add_action('init', 'create_page_tags_taxonomy');
Step 2: Add the Code Using Code Snippets
- Add a New Snippet:
- Go to Snippets > Add New.
- Give your snippet a descriptive title, like “Enable Separate Categories and Tags for Pages”.
- Copy and paste the code provided above into the Code field.
- Click “Save Changes and Activate”.
This will enable separate categories and tags specifically for WordPress pages, allowing for more granular control over your page organisation.
Conclusion
Adding categories and tags to WordPress pages can greatly benefit your site’s organisation, SEO, and user experience. Whether you choose to use the existing post taxonomies or create a separate category and tagging system for pages, the steps provided in this guide make the process straightforward. Use the Code Snippets plugin to easily implement these changes without touching your theme’s code, ensuring a smoother and safer customisation experience. Start categorising and tagging your pages today for a more structured and optimised WordPress site.
Does that sound too complicated, or you don’t have the time? Get in touch with us and one of our WordPress experts can do this for you.