Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #118210
    martha
    Participant

    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

    #118245
    tikaram
    Keymaster

    @mmarvel54 : How about using custom post types? I think it will be a better option if you can use custom post types.

    Regards,
    Tikaram

    #118264
    martha
    Participant

    how 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.

    #118299
    tikaram
    Keymaster

    @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 here

    Open 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

Viewing 4 posts - 1 through 4 (of 4 total)
  • The topic ‘add tags and categories to pages like with posts’ is closed to new replies.