In this article I am showing how to add the category and tags to pages.
First registering the category and tags taxonomy to “page” type by init
and register_taxonomy_for_object_type
functions. After adding this code “Categories” and “Tags” meta box will appear on Page Add/Edit Screen at Dashboard.
1 2 3 4 5 6 7 8 9 10 |
/** Enabling Categories and Tags Taxonomy for pages at Add/Edit Screen * * @since 1.0 * */ add_action('init', 'gd_register_category_tags_taxonomy_for_page'); function gd_register_category_tags_taxonomy_for_page() { register_taxonomy_for_object_type('post_tag', 'page'); register_taxonomy_for_object_type('category', 'page'); } |
Adding opening and closing footer markup. genesis_page_entry_footer_markup_open is creating the opening footer markup <footer class="entry-footer">
and genesis_page_entry_footer_markup_close is making the closing footer markup </footer>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
add_action( 'genesis_entry_footer', 'genesis_page_entry_footer_markup_open', 5 ); add_action( 'genesis_entry_footer', 'genesis_page_entry_footer_markup_close', 15 ); /** * Echo the opening structural markup for the entry footer. * * @since 2.0.0 * * @uses genesis_attr() Contextual attributes. */ function genesis_page_entry_footer_markup_open() { if ( 'page' === get_post_type() ) printf( '<footer %s>', genesis_attr( 'entry-footer' ) ); } /** * Echo the closing structural markup for the entry footer. * * @since 2.0.0 */ function genesis_page_entry_footer_markup_close() { if ( 'page' === get_post_type() ) echo '</footer>'; } |
Now display the page meta. genesis_post_meta function is executing for “post” type. I created a new function “genesis_page_meta” which is executing for “page” type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
add_filter( 'genesis_page_meta', 'do_shortcode', 20 ); add_action( 'genesis_entry_footer', 'genesis_page_meta' ); /** * Echo the page meta after the page content. * * The page info makes use of a couple of shortcodes by default, and the whole output is filtered via * `genesis_post_meta` before echoing. * * @since 0.2.3 * * @uses genesis_markup() Contextual markup. * * @return null Return early if on a page */ function genesis_page_meta() { if ( 'post' === get_post_type() ) return; $output = genesis_markup( array( 'html5' => '<p %s>', 'xhtml' => '<div class="post-meta">', 'context' => 'entry-meta-after-content', 'echo' => false, ) ); $output .= apply_filters( 'genesis_page_meta', '[post_categories] [post_tags]' ); $output .= genesis_html5() ? '</p>' : '</div>'; echo $output; } |
Here is the full snippet. You will add the following code in your functions.php file.
KarenC says
Sep. 29, 2021 – Used portions of code provided (didn’t need it all). Still works like a charm! Thanks Paul.
Chris says
Thank you for the code. It works like intended.
My only problem is that if I click on a category name or tag no content can be found. I get a message saying: “Sorry, no content matched your criteria.”
The archives for tags and categories on posts work fine.
How can I manage that tags and categories are shown on the same archive pages?
Many thanks in advance,
Chris
Paul says
try this PHP code once
Chris says
Works perfectly !
Thank you so much.
Chris says
Sorry, I was a little too quick with my feedback 😉
Your code snippet does work like intended but has the unfortunate side effect that my navigation menu disappears. Do you have a final tip for me how I get my primary navigation back?
Thanks in advance,
Chris
Paul says
replace this line
$query->set( ‘post_type’, array( ‘post’, ‘page’ ) );
WITH
$query->set( ‘post_type’, array( ‘post’, ‘page’, ‘nav_menu_item’ ) );
Chris says
That’s it! Thank you very much – again!!!
Simone Pietro Barbone says
Hi, great post here!
I have now meta dispalyed on pages but I would like to:
* Display categories above and not below the page content
* Change the label (e.g. from “File under” to “Categorie”)
For posts I’m doing that using Simple Edits Plugin.
How I could do that for pages as well please?
Many thanks in advance
S
Paul says
Replace line no 26, 27 & 28 with
]
And replace [post_categories] with [post_categories before="Categories "]
Beatty says
That is beautiful, man. Worked just how it should. Thank you for your code! So weird that my Gen child theme didn’t come with that. Are categories now bad for pages?