0

I was wondering if someone could help me out. I have used this function:

function add_taxonomies_to_pages() {

 register_taxonomy_for_object_type( 'category', 'page' );
 }
add_action( 'init', 'add_taxonomies_to_pages' );
 if ( ! is_admin() ) {
 add_action( 'pre_get_posts', 'category_and_tag_archives' );

 }
function category_and_tag_archives( $wp_query ) {
$my_post_array = array('post','page');

 if ( $wp_query->get( 'category_name' ) || $wp_query->get( 'cat' ) )
 $wp_query->set( 'post_type', $my_post_array );


}

It adds categories to my WordPress pages, but the category archive pages won't display. I have a category called Association Landing Pages with the slug association-landing-pages. I am developing locally. When I go to localhost/mywordpressitefolder/association-landing-page I am getting a page/file not found error.

1
  • Save permalink and try again and make sure .htaccess is correct.
    – emilushi
    Commented Oct 6, 2016 at 22:02

1 Answer 1

0

Your code did work for me; I was able to see pages under the category archive for Association Landing Pages at http://domain.com/category/association-landing-pages.

It sounds like you need to use the URL:

localhost/mywordpressitefolder/category/association-landing-pages

I noticed that you said that the slug was association-landing-pages and then you visited localhost/mywordpressitefolder/association-landing-page (without the s). Make sure to use the right URL based on your slug, and add /category too, as explained above.

I would also suggest putting your is_admin() check inside the category_and_tag_archives() function. I made a couple of other tweaks to the original code too:

function add_taxonomies_to_pages() {
    register_taxonomy_for_object_type( 'category', 'page' );
}
add_action( 'init', 'add_taxonomies_to_pages' );

function category_and_tag_archives( $wp_query ) {
    $my_post_array = array( 'post', 'page' );

    if ( ! is_admin() && is_category() && $wp_query->is_main_query() ) {
        $wp_query->set( 'post_type', $my_post_array );
    }
}
add_action( 'pre_get_posts', 'category_and_tag_archives' );

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.