从 Wordpress 中的自定义分类法中获取所有帖子 [英] Get all posts from custom taxonomy in Wordpress
问题描述
有没有办法从 Wordpress 中的分类法中获取所有帖子?
Is there a way to get all the posts from a taxonomy in Wordpress ?
在 taxonomy.php
中,我有这段代码可以从与当前术语相关的术语中获取帖子.
In taxonomy.php
, I have this code that gets the posts from the term related to the current term.
$current_query = $wp_query->query_vars;
query_posts( array( $current_query['taxonomy'] => $current_query['term'], 'showposts' => 10 ) );
我想创建一个页面,其中包含分类法中的所有帖子,而不考虑术语.
I'd like to create a page with all the posts in the taxonomy, regardless of the term.
是否有一种简单的方法可以做到这一点,或者我是否必须查询术语的分类法,然后循环遍历它们等.
Is there a simple way to do this, or do I have to query the taxonomy for the terms, then loop trough them, etc.
推荐答案
$myterms = get_terms('taxonomy-name', 'orderby=none&hide_empty');
echo $myterms[0]->name;
这样你就可以发布第一项,然后你就可以创建一个 foreach
;循环:
With that you'd post the first item, yo can then create a foreach
; loop:
foreach ($myterms as $term) { ?>
<li><a href="<?php echo $term->slug; ?>"><?php echo $term->name; ?></a></li> <?php
} ?>
这样你就可以列出它们,如果你想发布所有它们,-我的解决方案-在 foreach 中创建一个普通的 wordpress 循环,但它必须有类似的东西:
That way you'd list them, if you want to post all of them, -my solution- create a normal wordpress loop inside the foreach one, but it has to have something like:
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;
我发布了一些非常相似的内容 这里.
I posted something very similar here.
这篇关于从 Wordpress 中的自定义分类法中获取所有帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!