[wd_asp elements=’search’ ratio=’100%’ id=1]

Add Category and Tags to pages in WordPress

20th December 2018

Admin WP

wordpress category

A nice simple way to be able to add Category and Tags to pages without a plugin. Now you can add a category which is usually only available in post pages.


Add this to your functions.php file

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

function category_and_tag_archives( $wp_query ) {
if( $wp_query->is_main_query() && ! is_admin() && (
$wp_query->is_category() || $wp_query->is_tag() )) {
$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 );
}
}

add_action( 'init', 'add_taxonomies_to_pages' );
add_action( 'pre_get_posts', 'category_and_tag_archives' );