Shiba

Adventures in WordPress

  • Home
  • Dog
  • Art
  • Contact
  • WordPress Articles
    • WP Plugins
    • WP Programming
    • WP Admin Panels
    • WP Theme Design
    • WP How-To
    • WP Theme Images
You are here: Home / WordPress Programming / Add Tags and Categories to Your WordPress Page

Add Tags and Categories to Your WordPress Page

by ShibaShake 62 Comments

Currently, we cannot add tags and categories to our WordPress pages.

Why add tags to pages?

Tags are very useful. They can be applied in a variety of non-standard, but powerful ways to enhance our blogs.

I use tags to filter out file loads and certain types of plugin operations. For example, I only run the wp-syntax plugin functions on pages that actually contain code. Similarly, we can use tags to load in special galleries for particular pages, or restrict a given plugin to a specified group of posts.

This allows us to optimize our WordPress blog and increase page speed.

Tags and categories are also used by plugins to identify related posts (for example the YARPP plugin). Adding tags to pages will presumably help the plugin return better results for both related posts and pages.

How to Add Tags and Categories to WordPress Pages

We can add tags to our WordPress pages by simply using the add_meta_box command as follows –

// Add to the admin_init hook of your theme functions.php file
add_meta_box(	'tagsdiv-post_tag', __('Page Tags'), 'post_tags_meta_box', 
		'page', 'side', 'low');

Argument 1 must be set to tagsdiv-post_tag because this name is used in other WordPress functions to ensure that the metabox operates correctly and the proper tag values get saved with the page.

Argument 3 must be set to post_tags_meta_box because this is the native WordPress function used to render the tag metabox.

Similarly, we can use the same add_meta_box command to add a category metabox to our WordPress page.

add_meta_box(	'categorydiv', __('Categories'), 'post_categories_meta_box', 
		'page', 'side', 'core');

Added Tag and Category Metabox to our WordPress Page.
Added Tag and Category Metabox to our WordPress Page.

Saving Tags and Categories for WordPress Pages

You will quickly notice that when you update your WordPress page, the category does not get saved.

This is because the WordPress system does a check with the is_object_in_taxonomy function to see if categories can actually be assigned to pages.

To properly save our tag and category data, we want to properly register our WordPress pages to the post_tag and category taxonomies. We can do this with the register_taxonomy_for_object_type function.

// Add to the admin_init hook of your theme functions.php file

// Add tag metabox to page 
add_meta_box(    'tagsdiv-post_tag', __('Page Tags'), 'post_tags_meta_box', 
                'page', 'side', 'low');
register_taxonomy_for_object_type('post_tag', 'page');

// Add category metabox to page 
add_meta_box(    'categorydiv', __('Categories'), 'post_categories_meta_box', 
                'page', 'side', 'core');
register_taxonomy_for_object_type('category', 'page');                

Success! Once we do this, our categories get saved with our pages.

Note – As pointed out by XYDAC metaboxes automatically get added as part of the register_taxonomy_for_object_type function. Therefore, we can streamline our code and remove the add_meta_box commands.

// Add to the admin_init hook of your theme functions.php file
register_taxonomy_for_object_type('post_tag', 'page');
register_taxonomy_for_object_type('category', 'page');                

Categories are now properly attached to your WordPress page.
Categories are now properly attached to your WordPress page.

How to Retrieve Both Posts and Pages for Tag and Category Links

Now that we have set up tags and categories for WordPress pages, we may want our tag and category blog links to also retrieve those pages.

To do this, we can hook into the WordPress request filter.

// Add to the init hook of your theme functions.php file
add_filter('request', 'my_expanded_request');  

function my_expanded_request($q) {
	if (isset($q['tag']) || isset($q['category_name'])) 
                $q['post_type'] = array('post', 'page');
	return $q;
}

We Are Done!

Enjoy your new expanded WordPress pages.

Leave a Reply to ShibaShake Cancel reply

Your email address will not be published. Required fields are marked *

Comments

« Previous 1 2 3 4 Next »
  1. Johan van de Merwe says

    January 4, 2013 at 2:18 pm

    Works like a charm! Thank you.

    Reply
    • rasel says

      November 22, 2014 at 5:04 am

      how do you retrieve attached categories of front page?

      Reply
  2. John says

    October 12, 2012 at 9:32 pm

    The Code

    //Pages Tags & Category Meta boxes
    function add_pages_meta_boxes() {
    add_meta_box(	'tagsdiv-post_tag', __('Page Tags'), 'post_tags_meta_box', 'page', 'side', 'low');
    add_meta_box(	'categorydiv', __('Categories'), 'post_categories_meta_box', 'page', 'normal', 'core');
    }
    add_action('add_meta_boxes', 'add_pages_meta_boxes');
    
    add_action('init','attach_category_to_page');
    function attach_category_to_page() {
        register_taxonomy_for_object_type('category','page');
    }
    //end
    
    Reply
    • Simone says

      April 15, 2013 at 8:18 am

      Hi,
      I add your suggested code in my source code to add Category on my pages.
      Now, I’d like to create a sidebar that show pages of a similar Category.

      Can you help me, please?

      Thank you.
      Simone

      Reply
    • Maya says

      August 2, 2013 at 3:38 am

      Great! Thanks!

      Reply
    • Kar.ma says

      June 3, 2014 at 12:53 pm

      Thanks so much, this fixed my error (Call to undefined function add_meta_box()).

      Reply
  3. John says

    October 12, 2012 at 9:31 pm

    Here’s the completed code that works and saves the cats and tags.

    Reply
  4. colin says

    September 17, 2012 at 2:44 pm

    how do i show pages from one category? query posts doesn’t seem to go looking for pages. ??

    Reply
    • ShibaShake says

      September 18, 2012 at 9:53 pm

      One possibility is to hook into the ‘request’ filter.
      http://shibashake.com/wordpress-theme/mastering-the-wordpress-loop

      Reply
  5. maria says

    September 10, 2012 at 2:13 am

    I think I have the same problem than Jaap: It works fine, I can add tags and categories to the pages, but when I call the categories whithin the loop (with the_category()) to show them on the page they don’t appear, but tags do. Somebody knows how can I show them? Thanks

    Reply
  6. Geoffrey Hale says

    August 3, 2012 at 2:56 pm

    This is awesome! Thank you. Will our permalink structure settings apply to pages? Like if we set /%category%/%post%/, will that also result in /%category%/%page%/? Can we do this with Yoast’s SEO Plugin?

    Reply
    • ShibaShake says

      August 3, 2012 at 5:02 pm

      Will our permalink structure settings apply to pages?

      No. You will have to manually set this if you want to change page permalinks.

      Can we do this with Yoast’s SEO Plugin?

      Don’t know. I have never used this plugin.

      Reply
  7. QLStudio says

    May 19, 2012 at 8:33 am

    This seems to be play happy with latest WP:

    	// Add to the admin_init hook of your theme functions.php file
    	add_action( 'add_meta_boxes', 'qlstudio_add_page_tags' );
    	function qlstudio_add_page_tags() {
    		add_meta_box( 'tagsdiv-post_tag', __('Page Tags'), 'post_tags_meta_box', 'page', 'side', 'low' );
    	}
    	register_taxonomy_for_object_type('post_tag', 'page');
    
    Reply
    • Hugo says

      May 21, 2012 at 6:41 pm

      And how i display the tags on my theme?

      Reply
  8. Vivek says

    December 16, 2011 at 4:30 am

    Hi,

    Thanks for this code, i was looking for this kind of plugin where i can add categories with my pages. Although it is not exactly what i need but a bit helpful. I have a question :

    Is there any way using your code or some extra bit of code, we can retrieve only specific category and it’s subcategories and show on pages the same way you are showing on “add page” section ?

    Please help.

    Thanks
    Vivek

    Reply
    • ShibaShake says

      January 4, 2012 at 3:15 pm

      Yes. I would probably just copy the existing meta-box code for categories, and then alter that to only show the categories you want. Then use add_meta_box to add it to the page.

      Reply
  9. Brandon Bowman says

    November 15, 2011 at 11:56 am

    I am trying to implement this. I am using WordPress 3.2.1. When I add the code above to the functions.php, it returns an error Call to undefined function add_meta_box(). I am using the TwentyTen themes function.php. Is this just because I am putting it in the wrong place or what?

    Thanks

    Reply
    • ShibaShake says

      November 22, 2011 at 11:25 am

      It could be that you included the function call before the proper file was loaded. Check out the WordPress Codex for a detailed example of where to add the add_meta_box function call.
      http://codex.wordpress.org/Function_Reference/add_meta_box

      Reply
  10. vincent says

    November 7, 2011 at 9:34 am

    Hi !

    I would like , how to add Category meta box and my admin page ?
    I have developped my own plugin and i need to use WP category and i want to display div id= “categorydiv” in my admin_plugin page?
    Its possible ?

    Reply
    • ShibaShake says

      November 8, 2011 at 11:30 am

      http://shibashake.com/wordpress-theme/standard-wordpress-metabox

      Reply
« Previous 1 2 3 4 Next »

Recent Posts

  • Screenshot of an example article in code view of a modified Gutenberg editor.How to Harness the Power of WordPress Gutenberg Blocks and Combine It with Legacy Free-Form Text
  • Screenshot of the Success, WordPress has been installed page.Migrating Your WordPress Website to Amazon EC2 (AWS)
  • Screenshot of WinSCP for creating a SFTP configuration.How to Set-Up SFTP on Amazon EC2 (AWS)
  • WordPress Gutenberg code view screenshot of this article.How to Prevent Gutenberg Autop from Messing Up Your Code, Shortcodes, and Scripts
  • Screenshot of the Success, WordPress has been installed page.How to Create a WordPress Website on Amazon EC2 (AWS)

Recent Comments

  • Screenshot of the Success, WordPress has been installed page.How to Create a WordPress Website on Amazon EC2 (AWS) (1)
    • Erik
      - Great article. All worked great except for this step:apt install php-mysqlChanging to this fixed it:apt install ...
  • Add Custom Taxonomy Tags to Your WordPress Permalinks (125)
    • Anthony
      - Where does this code go? Like, what exact .php file please?
  • Screenshot of an example article in code view of a modified Gutenberg editor.How to Harness the Power of WordPress Gutenberg Blocks and Combine It with Legacy Free-Form Text (1)
    • tom
      - hi,my experience was like the same, but for me as theme developer the "lazy blocks"(https://wordpress.org/plugins/lazy-blocks/) ...
  • WordPress Custom Taxonomy Input Panels (106)
    • Phil T
      - This is unnecessarily confusing. Why declare a variable with the same name as your taxonomy? Why did you choose a taxonomy ...
  • Create Pop-up Windows in Your WordPress Blog with Thickbox (57)
    • Jim Camomile
      - I have used this tutorial several times and it is one of the few simple and effective ways to add popups with dynamic content, ...

Copyright © 2026 · Genesis Skins by ShibaShake · Terms of Service · Privacy Policy ·