Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
share|improve this question
    
why not use : codex.wordpress.org/Template_Tags/wp_list_pages and include exclude by getting id's of the pages by categories? –  woony Apr 10 '13 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? –  Chip Bennett Apr 11 '13 at 17:32
    
@ChipBennett sorry, I meant the category archive, not the search. –  Hinek Apr 12 '13 at 8:08

2 Answers 2

up vote 3 down vote accepted
+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.)

share|improve this answer
    
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 Apr 12 '13 at 8:14
    
Please see updates to the answer, which address your questions. –  Chip Bennett Apr 12 '13 at 13:47

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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