我已经玩了很久了,因为某种原因,我不能让它工作。
这是来自自定义帖子类型的单个帖子页面中。。。
基本上,我是通过使用get\\u the\\u术语获得自定义帖子类型的类别,然后通过foreach将其很好地输入到字符串中。
然后我使用WP\\u查询进行检索,但我认为我的逻辑有误。
这就是我所拥有的
// post types query
if ( have_posts() ) : while ( have_posts() ) :the_post();
// getting the categories
$categorynames = \'\';
$getcategory = get_the_terms($post->ID, \'custompostnamehere-categories\');
foreach($getcategory as $t){ $categorynames .= $t->term_id.\' ,\';}
if (substr($categorynames, -1) == \',\') {
$categorynames = substr($categorynames, 0, -1);
}
// end of post type query
endwhile; endif; wp_reset_query();
//starting new query to get related posts within the same categories
$args = array(
\'post_type\' => \'custompostnamehere\',
\'cat\' => $categorynames,
\'posts_per_page\' => 20
);
$q = new WP_Query($args);
// retrieving the data
while($q->have_posts()){
}
有人能告诉我我做错了什么吗。
谢谢
SO网友:Krzysiek Dróżdż
您正在对自定义分类法使用类别查询。所以它不起作用,因为没有具有此类ID的类别。
您应该改用tax\\u查询。
// post types query
while ( have_posts() ) {
the_post();
// getting the categories
$categoryIDs = array(); // it\'s much nicer than concatenated string
$getcategory = get_the_terms($post->ID, \'custompostnamehere-categories\');
foreach($getcategory as $t) {
$categoryIDs[] = $t->term_id;
}
// end of post type query
}
// resetting query isn\'t needed - you will call your own query next
//starting new query to get related posts within the same categories
$args = array(
\'post_type\' => \'custompostnamehere\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'yourcustomtaxonomy\',
\'field\' => \'id\',
\'terms\' => $categoryIDs
)
),
\'posts_per_page\' => 20
);
$q = new WP_Query($args);
// retrieving the data
while( $q->have_posts() ) {
$q->the_post();
...
}