2

I'm using "Map Categories to Pages" to add categories to pages.

When I call the link http://mypage.com/category/catA+catB I get all my articles, that are associated to the categories catA and catB. What I'm looking for is:

A) a way to do something similar with pages: call http://mypage.com/pages/catA+catB and get all pages that are associates to the categories catA and catB

-or-

B) a way to get first pages and then articles, on the category search result page, like:

  • Some Page
  • Another Page
  • Some Article
  • Another Article
  • One more Article
3
  • why not use : codex.wordpress.org/Template_Tags/wp_list_pages and include exclude by getting id's of the pages by categories?
    – woony
    Commented Apr 10, 2013 at 14:10
  • 1
    What is a "category search result" page? Category archive index pages and search results index pages are two entirely different things. To which one of the two are you referring? Commented Apr 11, 2013 at 17:32
  • @ChipBennett sorry, I meant the category archive, not the search.
    – Hinek
    Commented Apr 12, 2013 at 8:08

2 Answers 2

3
+50

To display pages in a category archive index:

  1. Add category taxonomy to the Page post-type

    Static pages, by default, do not have any taxonomies associated with them, including the category taxonomy. So you need to register the category taxonomy for the page post type, using register_taxonomy_for_object_type():

    function wpse94150_register_category_taxonomy_for_page_post_type() {
        register_taxonomy_for_object_type( 'category', 'page' );
    }
    add_action( 'admin_init', 'wpse94150_register_category_taxonomy_for_page_post_type' );
    
  2. Create Pages with Categories

    Self-explanatory. For pages to appear in a category archive index, you'll need to have pages that have categories assigned.

  3. Filter Query to include the page post-type for Category archive index pages

    Next, you need to tell WordPress to include the 'page' post-type in the results for the category archive index query, by filtering the $query object via pre_get_posts:

    function wpse94150_filter_pre_get_posts( $query ) {
        // Only modify the main loop query
        // on category archive index pages
        if ( $query->is_main_query && $query->is_category() ) {
            // Return both posts and pages
            $query->set( 'post_type', array( 'post', 'page' ) );
        }
    }
    add_action( 'pre_get_posts', 'wpse94150_filter_pre_get_posts' );
    
  4. View category archive index pages, now with posts and pages

    Navigate to example.com/category/cat-a, and if you have Pages assigned to "Cat A", you will see them in the archive index.

Edit

  1. Where do I enter the code from step 3?

I would put the code from step 3 in the same place you put the code from step 1: ideally in a site functionality Plugin, or as a less-ideal backup, in the functions.php file of a Child Theme.

And 2. Will this change be stable if I update WordPress or it's plugins?

Since these changes are not made directly to any core or Plugin file: yes, the changes are stable and future-proof. (Unless WordPress changes one of the underlying APIs or hooks, which is extremely doubtful.)

2
  • Thanks for the detailed answer, I've got two questions: 1. Where do I enter the code from step 3? And 2. Will this change be stable if I update WordPress or it's plugins?
    – Hinek
    Commented Apr 12, 2013 at 8:14
  • Please see updates to the answer, which address your questions. Commented Apr 12, 2013 at 13:47
0

Turned out, the "Map Categories to Pages" plugin has ist own Settings pages with an option "Show the Pages on Category pages", which does exactly what I want.

I marked Chip Bennett's answer anyway because it solves the problem without the plugin and it's very detailed. Thank's Chip.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.