To display pages in a category archive index:
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' );
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.
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' );
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
- 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.)