最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - category query for pages not working

programmeradmin2浏览0评论

I have added category support for pages

in functions.php:

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

I created the category 'saloane', I have pages with category 'saloane' added

in home.php:

$saloane_q = new WP_Query ( array('post_type' => 'page', 'category' => 'saloane' ));
if ($saloane_q->have_posts()) :
    while ($saloane_q->have_posts()) : $saloane_q->the_post();
        echo '<div class="f3">';    
        the_post_thumbnail('hr_medium');
        echo '<h2>';
            the_title();
        echo '</h2>';
        the_excerpt();
        echo '</div>';
    endwhile;
endif;
wp_reset_postdata();
?>

there should be only the pages with category 'saloane' shown, but instead all the pages are shown

I also tried:

$saloane_q = new WP_Query ( array('post_type' => 'page', 'category' => 11 ));

$saloane_q = new WP_Query ( array('post_type' => 'page', 'cat' => 11 ));

the same result.

with:

$saloane_q = new WP_Query ( array('post_type' => 'page', 'category_name' => 'saloane' ));

does not display anithyng

if I use it without post_type like this:

$saloane_q = new WP_Query ( array('category' => 11 ));

$saloane_q = new WP_Query ( array('cat' => 11 ));

it displays the posts.

I have added category support for pages

in functions.php:

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

I created the category 'saloane', I have pages with category 'saloane' added

in home.php:

$saloane_q = new WP_Query ( array('post_type' => 'page', 'category' => 'saloane' ));
if ($saloane_q->have_posts()) :
    while ($saloane_q->have_posts()) : $saloane_q->the_post();
        echo '<div class="f3">';    
        the_post_thumbnail('hr_medium');
        echo '<h2>';
            the_title();
        echo '</h2>';
        the_excerpt();
        echo '</div>';
    endwhile;
endif;
wp_reset_postdata();
?>

there should be only the pages with category 'saloane' shown, but instead all the pages are shown

I also tried:

$saloane_q = new WP_Query ( array('post_type' => 'page', 'category' => 11 ));

$saloane_q = new WP_Query ( array('post_type' => 'page', 'cat' => 11 ));

the same result.

with:

$saloane_q = new WP_Query ( array('post_type' => 'page', 'category_name' => 'saloane' ));

does not display anithyng

if I use it without post_type like this:

$saloane_q = new WP_Query ( array('category' => 11 ));

$saloane_q = new WP_Query ( array('cat' => 11 ));

it displays the posts.

Share Improve this question asked Apr 3, 2020 at 7:26 Botond VajnaBotond Vajna 4714 silver badges11 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I just re created the category. Deleted the old category and created a new category with the same name, and now it`s ok.

The pages were imported with the native WordPress import-export plugin, from another wp installation, with the category as well (the category was automatically created when I imported the pages), maybe something went wrong at the import-export process.

发布评论

评论列表(0)

  1. 暂无评论