wp query - Category Archive not working for pages

admin2025-01-08  4

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.

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.

Share Improve this question edited Oct 6, 2016 at 21:56 Dave Romsey 17.9k11 gold badges55 silver badges70 bronze badges asked Oct 6, 2016 at 14:32 steamfunksteamfunk 6453 gold badges13 silver badges26 bronze badges 1
  • Save permalink and try again and make sure .htaccess is correct. – emilushi Commented Oct 6, 2016 at 22:02
Add a comment  | 

1 Answer 1

Reset to default 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' );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736265983a1084.html

最新回复(0)