How to Add Categories and Tags to WordPress Pages

Category:
Date: November 6, 2024

The other day, I needed to add categories and tags to pages on one of my WordPress sites. Like many, I thought, "Aren't categories and tags just for blog posts?" It turns out that adding them to pages can be pretty useful.

Categories and tags help organise content. Categories are broad groups that link similar topics, while tags are specific labels that connect content, sharing something in common. We often use them for blog posts, but there's no reason pages can't benefit too.

Think about pages for services, guides, or events. Adding categories and tags can make things more organised and easier for visitors to find. It also helps search engines understand your site's structure better. Visitors can easily explore related content, improving their experience on your site.

In this post, I'll show you how to add categories and tags to WordPress pages, just like I did for my own site.

Category and tags on wordpress pages

Understanding Categories and Tags

Categories group content into broader topics, like sections in a library: Fiction, Non-Fiction, etc. Tags, meanwhile, are keywords that help connect individual pieces of content. Categories are usually hierarchical—you can have subcategories—while tags are not. Together, they allow visitors to find content more efficiently and make it easier for search engines to understand your site.

How They Work for Posts vs. Pages

WordPress usually limits categories and tags to posts, which makes sense if you run a blog. But if you have many pages about different topics, like services or guides, adding categories and tags can be just as helpful. It gives your pages the same organisation level as posts, making navigating and finding related content easier for visitors.

Benefits of Using Categories and Tags on Pages

Adding categories and tags to pages can improve navigation for visitors. It makes finding what they're looking for easier, leading to a better user experience. It also helps keep your content well-organised, grouping pages by relevant topics. This kind of organisation benefits search engines, too, which can lead to better SEO rankings.

How to Enable Categories and Tags for Pages

Method 1: Adding Code to functions.php

Before making changes, use a child theme or site-specific plugin to avoid losing edits during theme updates.

Step 1: Enable Categories

Add this to your theme's functions.php file:

function add_categories_to_pages() {
register_taxonomy_for_object_type('category', 'page');
}
add_action('init', 'add_categories_to_pages');

Step 2: Enable Tags

Add this code:

function add_tags_to_pages() { register_taxonomy_for_object_type('post_tag', 'page'); } add_action('init', 'add_tags_to_pages');

Step 3: Combine Both

To add both in one step:

function add_taxonomies_to_pages() { register_taxonomy_for_object_type('category', 'page'); register_taxonomy_for_object_type('post_tag', 'page'); } add_action('init', 'add_taxonomies_to_pages');

Step 4: Ensure UI Support

in menu

Make sure meta boxes for categories and tags appear in the page editor:

function add_taxonomy_meta_boxes_to_pages() { add_meta_box('categorydiv', __('Categories'), 'post_categories_meta_box', 'page', 'side', 'default'); add_meta_box('tagsdiv-post_tag', __('Tags'), 'post_tags_meta_box', 'page', 'side', 'default'); } add_action('admin_init', 'add_taxonomy_meta_boxes_to_pages');

Method 2: Creating a Custom Plugin

If you prefer not to edit theme files, create a plugin.

Step 1: Create Plugin File

Navigate to wp-content/plugins/, create a folder named page-taxonomies, and a file called page-taxonomies.php inside it.

Step 2: Add Plugin Code

Add this code to page-taxonomies.php:

<?php /** * Plugin Name: Page Taxonomies * Description: Enables categories and tags for pages. * Version: 1.0 * Author: Your Name */ if ( ! defined( 'ABSPATH' ) ) { exit; } function pt_add_taxonomies_to_pages() { register_taxonomy_for_object_type('category', 'page'); register_taxonomy_for_object_type('post_tag', 'page'); } add_action('init', 'pt_add_taxonomies_to_pages'); function pt_add_taxonomy_meta_boxes_to_pages() { add_meta_box('categorydiv', __('Categories'), 'post_categories_meta_box', 'page', 'side', 'default'); add_meta_box('tagsdiv-post_tag', __('Tags'), 'post_tags_meta_box', 'page', 'side', 'default'); } add_action('admin_init', 'pt_add_taxonomy_meta_boxes_to_pages');

Step 3: Activate Plugin

Log in to WordPress, go to Plugins > Installed Plugins, find Page Taxonomies, and activate it.

Displaying Categories and Tags on Pages

Add the following to your page.php template within the WordPress Loop to show categories or tags on pages.

Display Categories:

$categories = get_the_category(); if ( ! empty( $categories ) ) { echo '<div class="page-categories"><strong>Categories:</strong> '; foreach( $categories as $category ) { echo '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '">' . esc_html( $category->name ) . '</a>, '; } echo '</div>'; }

Display Tags:

$tags = get_the_tags(); if ( ! empty( $tags ) ) { echo '<div class="page-tags"><strong>Tags:</strong> '; foreach( $tags as $tag ) { echo '<a href="' . esc_url( get_tag_link( $tag->term_id ) ) . '">' . esc_html( $tag->name ) . '</a>, '; } echo '</div>'; }

Alternative: Plugins

Plugins are available that add categories or tags to pages without needing to code, but keep in mind that plugins can add bloat and slow down your site.

Common Issues and Troubleshooting

If categories and tags aren't showing up, it might be due to a theme or plugin conflict. Try deactivating plugins one at a time to see if the issue resolves, or switch to a default theme to check compatibility.

Dean Davis SEO & Web Design

Contact

07534 368169
dean@deandavis.co.uk
37 Borodin Close
Basingstoke
RG22 4EN
cross linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram