从Wordpress中的自定义分类中获取所有帖子
有没有办法从Wordpress中的分类中获取所有帖子?从Wordpress中的自定义分类中获取所有帖子
在taxonomy.php
,我有这个代码从当前术语相关的术语中获取帖子。
$current_query = $wp_query->query_vars;
query_posts(array($current_query['taxonomy'] => $current_query['term'], 'showposts' => 10));
我想创建一个包含分类中所有帖子的页面,而不管术语如何。
有一个简单的方法来做到这一点,或者我要查询的分类标准的条款,然后循环通过他们,等
$myterms = get_terms('taxonomy-name', 'orderby=none&hide_empty');
echo $myterms[0]->name;
有了,你会发布的第一个项目,哟然后可以创建一个foreach
;循环:
foreach ($myterms as $term) { ?>
<li><a href="<?php echo $term->slug; ?>"><?php echo $term->name; ?></a></li> <?php
} ?>
这样,你会一一列举了,如果你想发布所有的人,-My溶液 - 创建一个的foreach内正常的WordPress的循环,但它必须有这样的:
foreach ($myterms as $term) :
$args = array(
'tax_query' => array(
array(
$term->slug
)
)
);
// assigning variables to the loop
global $wp_query;
$wp_query = new WP_Query($args);
// starting loop
while ($wp_query->have_posts()) : $wp_query->the_post();
the_title();
blabla....
endwhile;
endforeach;
我发布了一些非常类似的东西here。
@PaBLoX提出了一个非常好的解决方案,但我自己做了一个解决方案,有点棘手,不需要每次查询每个条款的所有帖子。以及如果单个职位分配了多个任期,该怎么办?它会不会多次渲染相同的帖子?
<?php
$taxonomy = 'my_taxonomy'; // this is the name of the taxonomy
$terms = get_terms($taxonomy, 'orderby=count&hide_empty=1'); // for more details refer to codex please.
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'updates',
'field' => 'slug',
'terms' => m_explode($terms,'slug')
)
)
);
$my_query = new WP_Query($args);
if($my_query->have_posts()) :
while ($my_query->have_posts()) : $my_query->the_post();
// do what you want to do with the queried posts
endwhile;
endif;
?>
此功能m_explode
是我放进functions.php
文件的自定义功能。
function m_explode(array $array,$key = ''){
if(!is_array($array) or $key == '')
return;
$output = array();
foreach($array as $v){
if(!is_object($v)){
return;
}
$output[] = $v->$key;
}
return $output;
}
UPDATE
我们不需要这个定制m_explode
功能。 wp_list_pluck()
函数完全一样。所以我们可以简单地用wp_list_pluck()
代替m_explode
(参数相同)。干,对吗?
非常感谢!:)真的帮了我很多。 – Woppi 2017-05-11 07:32:22
在查询循环中,您可以收集数组中的所有后期引用,并在稍后使用新的WP_Query。
$post__in = array();
while ($terms_query->have_posts()) : $terms_query->the_post();
// Collect posts by reference for each term
$post__in[] = get_the_ID();
endwhile;
...
$args = array();
$args['post__in'] = $post__in;
$args['orderby'] = 'post__in';
$other_query = new WP_Query($args);
与帖子类型不同,WordPress没有为分类标准slug本身的路线。
为了使分类塞本身列出分配有分类的任何条款的所有帖子,您需要使用EXISTS
operator of tax_query
in WP_Query
:
// Register a taxonomy 'location' with slug '/location'.
register_taxonomy('location', ['post'], [
'labels' => [
'name' => _x('Locations', 'taxonomy', 'mydomain'),
'singular_name' => _x('Location', 'taxonomy', 'mydomain'),
'add_new_item' => _x('Add New Location', 'taxonomy', 'mydomain'),
],
'public' => TRUE,
'query_var' => TRUE,
'rewrite' => [
'slug' => 'location',
],
]);
// Register the path '/location' as a known route.
add_rewrite_rule('^location/?$', 'index.php?taxonomy=location', 'top');
// Use the EXISTS operator to find all posts that are
// associated with any term of the taxonomy.
add_action('pre_get_posts', 'pre_get_posts');
function pre_get_posts(\WP_Query $query) {
if (is_admin()) {
return;
}
if ($query->is_main_query() && $query->query === ['taxonomy' => 'location']) {
$query->set('tax_query', [
[
'taxonomy' => 'location',
'operator' => 'EXISTS',
],
]);
// Announce this custom route as a taxonomy listing page
// to the theme layer.
$query->is_front_page = FALSE;
$query->is_home = FALSE;
$query->is_tax = TRUE;
$query->is_archive = TRUE;
}
}
上面的例子就行了用'“分类” =>“$ term_name''需要像这样''taxonomy'=>“$ term_name”''双引号引用,或者更好的不引用像'taxonomy'=> $ term_name',或者甚至更好地省略先前的赋值, 'taxonomy'=> $ term-> slug'。也就是说,使用''tax_query'=> array(...)''显示的方法[已被弃用](http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters)。希望这可以帮助。 – MikeSchinkel 2013-05-15 00:43:53
对不起,延迟...你是对的。我相应地修改了我的答案:) – 2013-06-22 22:45:55
非常好!我希望我们一起努力帮助他人。 – MikeSchinkel 2013-06-23 04:37:56