- This topic has 3 replies, 2 voices, and was last updated 6 years, 9 months ago by tikaram.
-
AuthorPosts
-
June 27, 2017 at 11:14 am #118210marthaParticipant
Hi
I would like to add the same tags and categories to my pages like I have on my posts. Then on my main menu I use the categories and both my page/posts that relate to that category automatically show when a user clicks on the category listing in the menu.
This is what i tried. 1) add to functions.php 2) added custom field to page. Neither tag nor category is associated to pages yet.
// 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’);On each page added
query_args (custom field name) cat=id of category (argument)
so if category is “great stuff” and id is 3 i added cat=”3″
Appreciate any assistance. Thanks. Martha
June 28, 2017 at 2:25 am #118245tikaramKeymaster@mmarvel54 : How about using custom post types? I think it will be a better option if you can use custom post types.
Regards,
TikaramJune 28, 2017 at 9:24 am #118264marthaParticipanthow would i do that?
I could use a tag/category plugin for pages but did not want to keep adding plugins which slows down the site.
June 29, 2017 at 3:47 am #118299tikaramKeymaster@mmarvel54 : You will need to create a child theme and edit the functions.php of child theme. I suggest you not to edit the functions.php of the parent theme directly because during an update the changes made to your functions.php will be lost.
Download the child theme for clean journal Pro from hereOpen functions.php and add the following code in your child theme.
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' ); function category_and_tag_archives( $wp_query ) { if ( is_admin() ) { return; } $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( 'pre_get_posts', 'category_and_tag_archives' );
Upload the child theme and activate the child theme.
Let me know how this goes.Regards,
Tikaram -
AuthorPosts
- The topic ‘add tags and categories to pages like with posts’ is closed to new replies.