SNOW-10: Enable categories on pages and show the navigational sidebar on the About Us page

Metadata

Source
SNOW-10
Type
Task
Priority
Major
Status
Closed
Resolution
Fixed
Assignee
N/A
Reporter
Eloisa Guerrero
Created
2018-02-02T13:46:58.218-0500
Updated
2018-07-12T15:38:45.232-0400
Versions
N/A
Fixed Versions
N/A
Component
N/A

Description

By default pages do not have categories, so we have to programmatically enable them via the functions.php file. Once we've enabled the categories, we have to split sidebar.php into sidebar-post.php and sidebar-page.php to use the create_sidebar() function and show corresponding sidebars, specifically to enable this for the About Us page.

Comments

  • Eloisa Guerrero commented 2018-02-04T15:20:20.203-0500

    From https://stackoverflow.com/a/35087335

    In order for Wordpress to enable categories (and tags) support for pages, we need to register the taxonomy for those objects. Taxonomies refer to a way of grouping objects, and in our case, we want to group the object type of pages.

    After that they need to be included in the queries (access $query object) so that Wordpress knows to interact with this new addition to its taxonomy.

    Lastly, add_action is the hook that will launch the functions. The add_action hook with tag 'init' initialises the function 'tags_categories_support_all' at the time that Wordpress loads. 'pre_get_posts' gives the function access to the $query object (which is passed by reference, meaning they don't have to be declared or returned).

    // add tag and category support to pages
    function tags_categories_support_all() {
      register_taxonomy_for_object_type('post_tag', 'page');
      register_taxonomy_for_object_type('category', 'page');  
    }
    
    // ensure all tags and categories are included in queries
    function tags_categories_support_query($wp_query) {
      if ($wp_query->get('tag')) $wp_query->set('post_type', 'any');
      if ($wp_query->get('category_name')) $wp_query->set('post_type', 'any');
    }
    
    // tag and category hooks
    add_action('init', 'tags_categories_support_all');
    add_action('pre_get_posts', 'tags_categories_support_query');
    
  • Eloisa Guerrero commented 2018-02-04T15:35:37.673-0500

    I have since recycled the above code, just renaming the functions and putting the add_action hooks after their corresponding functions for easier reading.

    // Add tag and category support to pages
    function allow_pages_tags_categories() {
     register_taxonomy_for_object_type('post_tag', 'page');
     register_taxonomy_for_object_type('category', 'page'); 
    }
    add_action('init', 'allow_pages_tags_categories');
    // Ensure all tags and categories are included in queries
    function add_queries_tags_categories($wp_query) {
     if ($wp_query->get('tag')) $wp_query->set('post_type', 'any');
     if ($wp_query->get('category_name')) $wp_query->set('post_type', 'any');
    }
    add_action('pre_get_posts', 'add_queries_tags_categories');