在 WordPress 中按两个类别显示帖子?
Displaying posts by two category in WordPress?
我想让属于2个分类的post显示在我当前的post下,2个分类必须由显示的当前post决定,所以我写了这段代码但没有用,请帮忙,tnx。
<?php
$categories = get_the_category();
$category_id = $categories[0]->cat_ID;
$category_id2 = $categories[1]->cat_ID;
$query = new WP_Query( array('category__and' => ($category_id, $category_id2),
'post_type' => 'post',
'posts_per_page' => 4,
'orderby' => 'publish_date',
'order' => 'DESC',);
while($query->have_posts()) : $query->the_post();
?>
它抛出一个 php error
因为你忘记了最后一个 )
。
这应该有效:
$categories = get_the_category();
$category_id = $categories[0]->cat_ID;
$category_id2 = $categories[1]->cat_ID;
$query = new WP_Query(
array(
'category__and' => array( $category_id, $category_id2 ),
'post_type' => 'post',
'posts_per_page' => 4,
'orderby' => 'publish_date',
'order' => 'DESC',
),
);
while($query->have_posts()) : $query->the_post();
遇到这种情况尽量使用wp_debug
我想让属于2个分类的post显示在我当前的post下,2个分类必须由显示的当前post决定,所以我写了这段代码但没有用,请帮忙,tnx。
<?php
$categories = get_the_category();
$category_id = $categories[0]->cat_ID;
$category_id2 = $categories[1]->cat_ID;
$query = new WP_Query( array('category__and' => ($category_id, $category_id2),
'post_type' => 'post',
'posts_per_page' => 4,
'orderby' => 'publish_date',
'order' => 'DESC',);
while($query->have_posts()) : $query->the_post();
?>
它抛出一个 php error
因为你忘记了最后一个 )
。
这应该有效:
$categories = get_the_category();
$category_id = $categories[0]->cat_ID;
$category_id2 = $categories[1]->cat_ID;
$query = new WP_Query(
array(
'category__and' => array( $category_id, $category_id2 ),
'post_type' => 'post',
'posts_per_page' => 4,
'orderby' => 'publish_date',
'order' => 'DESC',
),
);
while($query->have_posts()) : $query->the_post();
遇到这种情况尽量使用wp_debug