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');
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');
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.
Old post. As of 3.0
add_action('init', 'add_category_metabox');
function add_category_metabox() {
register_taxonomy_for_object_type('category', 'page');
}
is enough
This is probably an obvious one, but can this code be used on a wp.com theme and editing under the customize–>css segment? Trying yet another shot in the dark here and any help would be appreciated.
No, this requires PHP code.
Hey I have created a category in pages section but how to display the pages who add on the particular category
i just cant add category and tag on firefox and chrome browser, if i try add tag and category with opera browser its work. i have reinstall and install firefox and remove all addon firefox but its still not working. I do deactive and active plugin wordpress that is the same, i cant add catergory and tag.
oh ya, this morning i have update wordpress with the last version 4.1
Hi
after adding the code, on the Category panel taking one Category at a time… couldn’t able to select multiple category for any single page.
Please suggest.
thanks
Sougata
How to show this categories on front end.
Thanks! This saves my life ^_^
I want to remove tags and categories from the home page. How do I do it?
Hey thanks Shiba for your great article. Anyways that was not what I am looking for. Can you tell me maybe how wordpress saves tag to post relations in the databse?
Thanks ๐
For some reason this code is causing my wp editor & page to be a white screen….any reason why it is doing this? Thanks for the help.
Likely, there was a syntax error, which can come from missing characters, adding the code in the wrong place, etc. When there is a hard error, the code stops, so the page does not get loaded. On my test site, I turn on WP debugging so that I can better track down the problem.
http://codex.wordpress.org/Debugging_in_WordPress
thx, works like a charm for me!
Fine, But My problem is i have added
Here every thing is working fine, But the tag count displaying at admin side post_tag page is incorrect it showing like 0 but on clicking on the count i have found some post which is not considered in count.
Could anybody help me to get out of this, i am getting tired while doing this. i will be very thankful to them. Please add reply as soon as possible..
Thank You in advance, : (
I want to add tag only on page of wordpress v4.5.2. By using the following code, gets tag in admin setting, but the tag doesn’t show up on page. How come?
// add tag support to pages
function tags_support_all() {
register_taxonomy_for_object_type(‘post_tag’, ‘page’);
}
// ensure all tags are included in queries
function tags_support_query($wp_query) {
if ($wp_query->get(‘tag’)) $wp_query->set(‘post_type’, ‘any’);
}
// tag hooks
add_action(‘init’, ‘tags_support_all’);
add_action(‘pre_get_posts’, ‘tags_support_query’);
Please help. Thanks in advance!
Using on a new project, works great, thank you ShibaShake for the post and John for “the code”!
Hi shiba,
This is exactly what I have been looking for. But I don’t seem to be able to get it to work. I’ve added the code to the functions.php in my child theme. I’ve got a template called “post.php” which is based on:
http://voodoopress.com/how-to-post-from-your-front-end-with-no-plugin/
I can’t seem to get the meta boxes to show. I’ve added the following div’s to post.php:-
TAG
Category
I’ve tried changing “init” to “admin_init”… but still no luck. Page just would show the meta boxes. (I’m using WordPress 3.5) What am I doing wrong?
Thank you and regards,
Dinesh, S.
I would try doing it on a standard setup first with a standard theme.
Works like a charm! Thank you.
how do you retrieve attached categories of front page?
The Code
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
Great! Thanks!
Thanks so much, this fixed my error (Call to undefined function add_meta_box()).
Here’s the completed code that works and saves the cats and tags.
how do i show pages from one category? query posts doesn’t seem to go looking for pages. ??
One possibility is to hook into the ‘request’ filter.
http://shibashake.com/wordpress-theme/mastering-the-wordpress-loop
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
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?
No. You will have to manually set this if you want to change page permalinks.
Don’t know. I have never used this plugin.
This seems to be play happy with latest WP:
And how i display the tags on my theme?
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
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.
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
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
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 ?
http://shibashake.com/wordpress-theme/standard-wordpress-metabox
Hi,
Could someone tell me how do i retrieve all tags that are posted while adding pages?
Thanks,
Harpinder
wp_get_object_terms
http://codex.wordpress.org/Function_Reference/wp_get_object_terms
Hi
Any chance you could elaborate on how to display on a page the Category ID’s that have been selected.
I’ve looked at the codex and I just don’t get it ๐
I can’t get wp_get_object_terms to return me anything, the other stuff sets and saves brilliantly though….
Half way there ๐
Hereโs an example on retrieving the tags attached to a page (in my case $work): — the good example —
Great Info! Really helpful to me. thanks for posting…
Where is the admin_init hook located? I cant find it in the functions php
What theme are you using?
I’m also having trouble finding the admin_init hook. Can you have a look at my function.php file I have shared as google doc under my website field in this form and let me know where to put it. Thanks
That admin_init hook could also be in any of the included files. Or if there isn’t an admin_init hook, then you can add one using the add_action function.
Great article – very helpful. I can now get my pages to display its tags, but not its categories – is there any reason category template tags (ie the_category() wouldn’t work with this solution?
Thanks!
If we do these changes will auto tagging plugins like Strictly Auto Tags tag the content of pages?
That will depend on the plugin and whether it checks for taxonomy relationships with various custom post types. It is best to ask the plugin developer.
Shiba, thanks for sharing this– I’ve been looking for a plug-in that does this but couldn’t get one. I am still having a slight issue, hoping someone could help: I added the two lines register_taxonomy of code in the initialization function. This made the Category and Tag boxes show on my page edit screen, however the categories don’t seem to paste. What am I doing wrong?
Thanks in advance!
When you say the categories don’t seem to paste, do you mean they do not get saved? Do the tags get saved?
I’m pretty sure I’ve tried this before and it would work, but now my categories don’t seem to save.
The tags save just fine.
Any ideas?
It saves categories and tags for pages.But when you want to display it in loop you have to add ‘post_type’ => ‘page’ with quotes under query_post array.If you don’t do that wordpress thinks user wants posts in a category not pages.
I used ‘taxonomies’ => array(‘category’), to get the categories meta box on my custom post page, for some reason the categories do not seem to be updating (when I erase a category it is still there as an option on my custom post page). Do you know why? This is the code I used:
Hmmm, it looks right to me. The bug may be coming from somewhere else.
Stumped. Tried adding your code to functions.php (within the twentyten theme), using WP 3.0.1. Nothing changed in my page in the admin UI. Suggestions?
Hmmm, strange. Try adding it in the admin_init action hook.
I had the same problem as Michael and adding it in the admin_init action hook worked. Thanks!
Hey i think after using register_taxonomy_for_object_type(), WordPress automatically creates respective Meta-box,so why do you need to add them.
We can use only register_taxonomy_for_object_type(),and that should be all, adding the request filter was important as WordPress doesn’t automatically links pages to categories in WP 3.0
Nice Work ๐
Thanks for letting me know. I have included it in the article. Great catch!