How to Add Categories and Tags to Pages in WordPress

By default WordPress does not allow a user to add categories to Pages. However adding categories and tags to Pages is quite simple.

In this post I will share a code snippet which allows you to add Post categories to Pages. Here is what we will be doing:

  • We will link Category and Tag Taxonomy to the Page
  • Modify WordPress Query to display List of Pages using Category Archive and Tag Archive.
  • Add the Snippet to our WordPress Website

You can also achieve the same result by using this plugin 

It sounds difficult but in reality its quite simple :-). Lets start

Assign Category and Tags to WordPress Page.

WordPress allows you to assign taxonomy to any post_type. Practically speaking, Pages are also a Custom Post Type. So assigning a Taxonomy to a Page is quite simple. Here is the snippet.

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

As you can see we linked Category and Post_Tag to Pages. Both Category and Post_tag are Default taxonomy which are by default linked to the Post. The above snippet links both these taxonomies to the ‘Page’. The Page Editing Screen will now display List of Categories to choose from

Add Category to Page WordPress
Category Selection Box in Page Editing Screen

Customize WP query

We can now add Categories and Tags to our Pages. However we still cannot display a list of pages based on Category or Tags. So now we will add some code so that  we can use the Post Archive Template to display a list of Pages based on categories. Here is the snippet.

if ( ! is_admin() ) {
 add_action( 'pre_get_posts', 'category_and_tag_archives' );
 
 }
function category_and_tag_archives( $wp_query ) {
$my_post_array = array('post','page');
 
 if ( $wp_query->get( 'category_name' ) || $wp_query->get( 'cat' ) )
 $wp_query->set( 'post_type', $my_post_array );
 
 if ( $wp_query->get( 'tag' ) )
 $wp_query->set( 'post_type', $my_post_array );
}

The above snippet modifies the Wp_Query variable. The above snippet hooks in pre_get_posts and adds page to the post type variable.  A couple of things to note

  • Since we only want to modify the query for archive.php and tag.php template, we  check for the presence of tag or category query variable.
  • We make sure that the query is not modified in the admin view.  We use the !is_admin() check for this.

Here is the complete snippet.

function add_taxonomies_to_pages() {
 register_taxonomy_for_object_type( 'post_tag', 'page' );
 register_taxonomy_for_object_type( 'category', 'page' );
 }
add_action( 'init', 'add_taxonomies_to_pages' );
 if ( ! is_admin() ) {
 add_action( 'pre_get_posts', 'category_and_tag_archives' );
 
 }
function category_and_tag_archives( $wp_query ) {
$my_post_array = array('post','page');
 
 if ( $wp_query->get( 'category_name' ) || $wp_query->get( 'cat' ) )
 $wp_query->set( 'post_type', $my_post_array );
 
 if ( $wp_query->get( 'tag' ) )
 $wp_query->set( 'post_type', $my_post_array );
}

Inserting the Snippet.

Now we need to add this snippet to our WordPress Website . There are two ways to go about it.

Adding the Snippet in Theme’s function.php file.

This method is simple but not recommended. In case you switch the theme , all the changes made will be lost

Utilizing the Code Snippets Plugin.

Utilizing the Code Snippets Plugin is the recommended method. (Download Link)

The code snippet plugin allows you to add php snippets without modifying any code. So in future if you switch your theme provider, the customization will not break.   Another benefit is that you can easily export the snippets.

Conclusion

Adding Category support to Pages is quite simple.  Now you can easily organize Pages just like you organize Posts.

Use a Plugin

So I took the code and released it as a free plugin. Here is the link

Be more productive. Visit OfficeBeginner for latest tutorials on Ms Office and Google Suite

38 thoughts on “How to Add Categories and Tags to Pages in WordPress”

  1. Had a problem with the snippet I added via Code Snippets, was able to disable using your advice. Some advice on how to modify your example for what the user wants to do would be nice.

    Reply
  2. Another way, and maybe the best way, is to create a child theme with a functions.php and insert the above snippet into it. A child theme requires a style.css so you can also make styling changes in it and nothing in your child theme will get overwritten by an update.
    Thanks for the snippet, saved me a trip to the codex. It has been filed for future reference and will most likely be put in place on every site I build. (NOTE*, Your child functions.php needs an opening php tag before this snippet) Check the wordpress codex for more on creating a child theme. It’s one thing there that’s easy to find. Also, child themes don’t work on every theme, though they should. If a theme won’t take a child theme, I won’t use it.

    Reply
    • Great idea of using this with child theme and functions.php! I did this and it worked well for me. I just started using a Child theme the last few months, mainly to edit style.css and I’m very pleased with it. This is my first venture into having a functions.php with my child theme.
      Great snippet and tip!

      Reply
  3. I used the complete snippet given in the plugin code snippets as advised and it broke my site. Anytime I click anywhere now it says:

    Fatal error: Cannot redeclare add_taxonomies_to_pages() (previously declared in /home/content/p3pnexwpnas02_data02/69/2724069/html/wp-content/plugins/add-category-to-pages/add-category-to-page.php:14) in /home/content/p3pnexwpnas02_data02/69/2724069/html/wp-content/plugins/code-snippets/includes/snippet-ops.php(446) : eval()’d code on line 4

    Can anyone give me advise for how to fix it?

    Reply
    • It sounds like a function doing the same or similar job is being used elsewhere on your site. You can either switch off the other offending plugin, or just call your function something else:

      function add_categories_to_my_pages() {
      register_taxonomy_for_object_type( ‘post_tag’, ‘page’ );
      register_taxonomy_for_object_type( ‘category’, ‘page’ );
      }
      add_action( ‘init’, ‘add_categories_to_my_pages’ );

      Reply
      • I received the same error. I added the snippet and this happens to my entire site

        Fatal error: Cannot redeclare add_taxonomies_to_pages() (previously declared in /home3/rubyenceeg/public_html/wp-content/plugins/add-category-to-pages/add-category-to-page.php:14) in /home3/rubyenceeg/public_html/wp-content/plugins/code-snippets/php/snippet-ops.php(384) : eval()’d code on line 4

        Reply
        • Function names often clash as ideally you should describe what the function actually does and if you’ve got two bits of script doing the same thing then it’s likely they’ll have similar if not the same names. WordPress introduced this functionality to the core which broke a few sites with plugins already doing the job. Good practice when writing a plugin is using characters at the start of each function i.e. abc_ so there shouldn’t be a clash. Your initials or the initials of your company should suffice as a differential.

          Reply
  4. Hello, we have installed the plugin to view pages by category order, but how can we change the order of the pages in the feed? We would like to be able to change that if possible.
    Please can you advise.

    Reply
    • Did you ever figure out how to do this? I have the same question – I want to change the order in which pages are displayed on a category page

      Reply
  5. Hi Ankit,
    Your code solved a huge problem I was having displaying pages in category.php. Thanks to your fantastic snippet everything works just fine now. I have just one question, how would I modify your snippet so that I can eliminate a specific category as well as parent pages from displaying in the archive? I know your plugin states that it has the capability of eliminating categories but I prefer to use the snippet rather than plugin. Thanks!

    Reply
  6. Hi there,

    I was wondering if it’s possible to display the page categories on the page. I tried using the wordpress categories widget but that will display ALL categories that are being used, I want to display categories on related to the page on each page (like posts) is this possible?

    thank you,
    Graziella

    Reply
  7. Hi, great advice, thank you. Just one comment. The code works perfectly for me. I install the plugin but it didnt worked.
    Thanks again, you solved a really big issue I was having in my website.

    Reply
  8. Had a similar error as above:
    Fatal error: Cannot redeclare add_taxonomies_to_pages() (previously declared in /home/doppelbe/public_html/wp-content/themes/foodica-child/functions.php:18) in /home/doppelbe/public_html/wp-content/themes/foodica/functions/functions.php on line 342

    Tried changing the name but didn’t help, any other recommendations?

    Thanks,

    Louis

    Reply
  9. BLESS YOU!
    I’ve tried multiple plugins – which have allowed me to add categories to pages, even manual excerpts – none of them would show up in the actual archive loops. The snippet was missing. This is so helpful. Thank you!

    Reply
  10. Hi,
    The script works well, you think it’s possible to divide pages categories to post scategories in wordpress backend?
    Now if I open to modify or add a page in backend I see all the categories in right column (pages categories and posts categories) while it would be better to show only categories related to pages.
    Thank you!

    Reply
  11. Not at all helpful to say ‘download plugin’ for beginners who are most likely not going to have downloaded wordpress and are therefore not able to install said plugin. Here is some short code for those like me that have been scouring the web for something that actually works on regular pages.

    First, make sure you have your pages in your menu set up properly.
    YOU DO NOT NEED TO MAKE YOUR PAGES ‘Categories’ or ‘Tags’!
    This wouldn’t give you actual pages to then go and edit, so if you are wanting to add sliders, text, an intro, or anything for that matter, you wouldn’t be able to.

    Then go to WP Admin > Pages
    Select the text editor instead of visual editor (far right handside tab)

    Then past the following short code:

    [display-posts category=”hair,makeup,reviews,beauty” posts_per_page=”10″ include_date=”true” text-decoration: none date_format=”F j, Y” order=”DESC” include_excerpt=”true” wrapper=”div” image_size=”large”]
    <

    (The shortcode collects all the posts that you have assigned certain categories i.e. mine was hair and beauty. It then allocates how many posts (mine was 10), the date (in descending order,) with a large image and an excerpt of the post)

    Reply
    • Hello Anne,
      i read your post and tried to use your code ( with my categories name ) but it displays all the post with content on the page :/

      what i need: is there any way that i can show names (titles) of categories (i’ve 5 categories now) on top of every page in WordPress?

      pardon me if i sound too noob ( not so techh savvy )

      any help will be much appreciated 🙂

      Reply
  12. The snippet works well with wp 4.8
    But pages with category are (most often) not supported as posts
    with categories. For example, archive for category X will take
    all posts and exclude all pages with that same category.
    Simply because the code looks for posts only.

    In short, the snippet is a good start in a correct direction.

    Reply
  13. Installed the plugin and the snippet and added some pages to categories but there is a problem. I created a custom menu to display my pages in categories but when you click on the category, it just displays “category archive” and it’s empty. It shouldn’t be empty because the pages are assigned to them but the menu can’t see them? So this makes it useless for me unless there is a solution to this problem

    Reply
  14. Thanks Ankit for this article. It works.
    However, I would like to do the opposite. This is assigning various pages to categories, but I would like to assign categories to pages.

    For example, if I create a page “A” and a category “B”, everytime I create a post and connect it to “B”, it automatically shows on the website on the A page.

    I want to do this because when I want all my menu options to be posts and not categories.

    Reply
  15. Hi Ankit- your plug in is working well for me, thanks. I would like to know how I can have more control over what is displayed once a Category has been chosen (the results). I would like to just display the page titles rather than the entire page content. Is this possible? Thanks, Shelley

    Reply
  16. Thanks so much for this tutorial. Been looking for a way to add categories to my pages and this helped me. And as someone suggested, it’s best to create a child theme and add the code snippet to the functions.php in the child theme so you don’t lose it when you update your theme. Great guide and thanks once again.

    Reply
  17. As a person that knows squat about this stuff, really simplified, detailed instructions with pictures would be helpful. How, exactly, do I use it?
    I have the plugin installed and activated but where is it? How do I use it if I can’t see it?
    Clueless.

    Reply
  18. Just wondering: does this plugin add the categories to the permalink structure too, just like it can be done with blog post permalinks?

    Reply

Leave a Comment