按分类列出所有自定义帖子类型的帖子
5 个回答
- 投票数
-
- 2012-09-25
尝试一下
$custom_terms = get_terms('custom_taxonomy'); foreach($custom_terms as $custom_term) { wp_reset_query(); $args = array('post_type' => 'custom_post_type', 'tax_query' => array( array( 'taxonomy' => 'custom_taxonomy', 'field' => 'slug', 'terms' => $custom_term->slug, ), ), ); $loop = new WP_Query($args); if($loop->have_posts()) { echo '<h2>'.$custom_term->name.'</h2>'; while($loop->have_posts()) : $loop->the_post(); echo '<a href="'.get_permalink().'">'.get_the_title().'</a><br>'; endwhile; } }
我们获得了分类法的所有术语,遍历它们,并触发了一个标题链接,指向该术语的每个帖子.如果您需要对分类术语进行重新排序,则可以使用一个插件轻松完成.我相信,重新分类. 但是请注意,此插件会在激活上向表中添加(!)另一列,并且在停用时不会将其删除!
Try this
$custom_terms = get_terms('custom_taxonomy'); foreach($custom_terms as $custom_term) { wp_reset_query(); $args = array('post_type' => 'custom_post_type', 'tax_query' => array( array( 'taxonomy' => 'custom_taxonomy', 'field' => 'slug', 'terms' => $custom_term->slug, ), ), ); $loop = new WP_Query($args); if($loop->have_posts()) { echo '<h2>'.$custom_term->name.'</h2>'; while($loop->have_posts()) : $loop->the_post(); echo '<a href="'.get_permalink().'">'.get_the_title().'</a><br>'; endwhile; } }
We get all the terms of a taxonomy, loop through them, and fire off a title link to each post that belongs to that term. If you need to reorder the taxonomy terms, you can do so with a plugin pretty easily. Reorder Taxonomy, I believe. But pay attention that this plugin adds(!) another column to your table on activation and does not remove it upon deactivation!
-
@GhostToast,您好:我的工作很不错,我有一个直接的问题,我该如何按分类法进行过滤,我有网球,高尔夫,足球,排球,这些代码将其与所有经过分类检查的帖子一起带给他们,如何过滤以仅显示带有帖子的足球分类法.Hi @GhostToast This works great, I have a direct question, how can I filter this by taxonomy, I have tennis, golf, soccer, voleyball, this codes brings them all with their post that have the taxonomy checked, How can I filter to only show the Soccer Taxonomy with its posts.
- 0
- 2017-03-07
- Rodrigo Zuluaga
-
@RodrigoZuluaga那将是一个基本的单个查询.带走`$ custom_terms`和`foreach()`,然后手动为子弹或您想要的任何东西定义`'terms'.@RodrigoZuluaga that would be a basic single query then. take away `$custom_terms` and the `foreach()` and just define `'terms'` manually to the slug or whatever you want.
- 0
- 2017-03-07
- GhostToast
-
我觉得没有什么不同,但是您的代码该死的很好 $ args=array('post_type'=>'publica', 'tax_query'=>数组( 数组( 'taxonomy'=>'comision-publicaciones', 'field'=>'name', 'terms'=> array($ter_name) ), ), );I got it little different I think but your code hell good $args = array('post_type' => 'publica', 'tax_query' => array( array( 'taxonomy' => 'comision-publicaciones', 'field' => 'name', 'terms' => array($ter_name) ), ), );
- 0
- 2017-03-08
- Rodrigo Zuluaga
-
- 2012-09-25
这不是一个特别优雅的解决方案,但是您可以针对特定的术语分别创建多个查询,然后将其输出.希望有人可以提出一种更好的自动拉动条件以修改输出/排序的方法.但这会让你前进.
<?php //First Query for Posts matching term1 $args = array( 'tax_query' => array( array( 'taxonomy' => 'taxonomy_1', 'field' => 'slug', 'terms' => array( 'term1' ) ), ), 'post_type' => 'my-post-type' ); $query = new WP_Query( $args ); if ( have_posts() ) { $term = $query->queried_object; echo 'All posts found in ' . $term->name; while ( have_posts() ) : the_post(); //Output what you want the_title(); the_content(); endwhile; } //RESET YOUR QUERY VARS wp_reset_query(); //Second Query for term2 $args = array( 'tax_query' => array( array( 'taxonomy' => 'taxonomy_1', 'field' => 'slug', 'terms' => array( 'term2' ) ), ), 'post_type' => 'my-post-type' ); $query = new WP_Query( $args ); if ( have_posts() ) { $term = $query->queried_object; echo 'All posts found in ' . $term->name; while ( have_posts() ) : the_post(); //Output what you want the_title(); the_content(); endwhile; }
Not a particularly elegant solution but you can create multiple queries each for the specific terms and then output them. Hopefully someone can come up with a nicer way of automatically pulling the terms to modify the output/sorting. But this would get you going.
<?php //First Query for Posts matching term1 $args = array( 'tax_query' => array( array( 'taxonomy' => 'taxonomy_1', 'field' => 'slug', 'terms' => array( 'term1' ) ), ), 'post_type' => 'my-post-type' ); $query = new WP_Query( $args ); if ( have_posts() ) { $term = $query->queried_object; echo 'All posts found in ' . $term->name; while ( have_posts() ) : the_post(); //Output what you want the_title(); the_content(); endwhile; } //RESET YOUR QUERY VARS wp_reset_query(); //Second Query for term2 $args = array( 'tax_query' => array( array( 'taxonomy' => 'taxonomy_1', 'field' => 'slug', 'terms' => array( 'term2' ) ), ), 'post_type' => 'my-post-type' ); $query = new WP_Query( $args ); if ( have_posts() ) { $term = $query->queried_object; echo 'All posts found in ' . $term->name; while ( have_posts() ) : the_post(); //Output what you want the_title(); the_content(); endwhile; }
-
- 2014-07-10
好人!我一直在寻找GhostOne的解决方案. 在我的情况下,自定义帖子类型为"minining_accidents",与此相关的自定义分类法为" accident-types",其中包含多个术语.我的想法是创建一个自定义窗口小部件,以显示此自定义分类法中的术语列表.在我的试运行中,它得到了我想要的.休息休息了.这是我的代码:
function fn_get_list_of_mining_accident_types() { $custom_taxonomy='accident-types'; $custom_terms = get_terms($custom_taxonomy); $str_return='<ul>'; foreach($custom_terms as $custom_term) { wp_reset_query(); $args = array( 'post_type' => 'minining_accidents', 'tax_query' => array( array( 'taxonomy' => $custom_taxonomy, 'field' => 'slug', 'terms' => $custom_term->slug, ), ), ); $loop = new WP_Query($args); $term_name=$custom_term->name; $term_slug=$custom_term->slug; $term_link=get_term_link($term_slug, $custom_taxonomy); $str_return.='<li><a href="'.$term_link.'">'.$term_name.'</a>'; if($loop->have_posts()) { $str_return.='<ol>'; while($loop->have_posts()) : $loop->the_post(); $str_return.='<li><a href="'.get_permalink().'">'.get_the_title().'</a></li> '; endwhile; $str_return.='</ol>'; } $str_return.='</li>'; } $str_return.='</ul>'; return $str_return; }
是的!总有一个选项可以进一步改进代码.
Nice one! GhostOne's solution was what I had been looking for. In my situation the custom post type was 'minining_accidents' and custom taxonomies associated with this was 'accident-types' which had multiple terms under it. My idea was to create a custom widget to show list of posts under terms in this custom taxonomies. In my trial run it got what I wanted. Rest was spruce up. Here is my code:
function fn_get_list_of_mining_accident_types() { $custom_taxonomy='accident-types'; $custom_terms = get_terms($custom_taxonomy); $str_return='<ul>'; foreach($custom_terms as $custom_term) { wp_reset_query(); $args = array( 'post_type' => 'minining_accidents', 'tax_query' => array( array( 'taxonomy' => $custom_taxonomy, 'field' => 'slug', 'terms' => $custom_term->slug, ), ), ); $loop = new WP_Query($args); $term_name=$custom_term->name; $term_slug=$custom_term->slug; $term_link=get_term_link($term_slug, $custom_taxonomy); $str_return.='<li><a href="'.$term_link.'">'.$term_name.'</a>'; if($loop->have_posts()) { $str_return.='<ol>'; while($loop->have_posts()) : $loop->the_post(); $str_return.='<li><a href="'.get_permalink().'">'.get_the_title().'</a></li> '; endwhile; $str_return.='</ol>'; } $str_return.='</li>'; } $str_return.='</ul>'; return $str_return; }
Yes! There always is an option to further improve the code.
-
- 2020-06-14
这是我在进入此线程之前尝试的长期解决方案.希望这可以帮助某人更好地理解.
<?php // Get list of all taxonomy terms -- In simple categories title $args = array( 'taxonomy' => 'project_category', 'orderby' => 'name', 'order' => 'ASC' ); $cats = get_categories($args); // For every Terms of custom taxonomy get their posts by term_id foreach($cats as $cat) { ?> <a href="<?php echo get_category_link( $cat->term_id ) ?>"> <?php echo $cat->name; ?> <br> <?php // echo $cat->term_id; ?> <br> </a> <?php // Query Arguments $args = array( 'post_type' => 'portfolio', // the post type 'tax_query' => array( array( 'taxonomy' => 'project_category', // the custom vocabulary 'field' => 'term_id', // term_id, slug or name (Define by what you want to search the below term) 'terms' => $cat->term_id, // provide the term slugs ), ), ); // The query $the_query = new WP_Query( $args ); // The Loop if ( $the_query->have_posts() ) { echo '<h2> List of posts tagged with this tag </h2>'; echo '<ul>'; $html_list_items = ''; while ( $the_query->have_posts() ) { $the_query->the_post(); $html_list_items .= '<li>'; $html_list_items .= '<a href="' . get_permalink() . '">'; $html_list_items .= get_the_title(); $html_list_items .= '</a>'; $html_list_items .= '</li>'; } echo $html_list_items; echo '</ul>'; } else { // no posts found } wp_reset_postdata(); // reset global $post; ?> <?php } ?>
This is long solution i tried before coming to this thread. Hope this may help someone to understand better.
<?php // Get list of all taxonomy terms -- In simple categories title $args = array( 'taxonomy' => 'project_category', 'orderby' => 'name', 'order' => 'ASC' ); $cats = get_categories($args); // For every Terms of custom taxonomy get their posts by term_id foreach($cats as $cat) { ?> <a href="<?php echo get_category_link( $cat->term_id ) ?>"> <?php echo $cat->name; ?> <br> <?php // echo $cat->term_id; ?> <br> </a> <?php // Query Arguments $args = array( 'post_type' => 'portfolio', // the post type 'tax_query' => array( array( 'taxonomy' => 'project_category', // the custom vocabulary 'field' => 'term_id', // term_id, slug or name (Define by what you want to search the below term) 'terms' => $cat->term_id, // provide the term slugs ), ), ); // The query $the_query = new WP_Query( $args ); // The Loop if ( $the_query->have_posts() ) { echo '<h2> List of posts tagged with this tag </h2>'; echo '<ul>'; $html_list_items = ''; while ( $the_query->have_posts() ) { $the_query->the_post(); $html_list_items .= '<li>'; $html_list_items .= '<a href="' . get_permalink() . '">'; $html_list_items .= get_the_title(); $html_list_items .= '</a>'; $html_list_items .= '</li>'; } echo $html_list_items; echo '</ul>'; } else { // no posts found } wp_reset_postdata(); // reset global $post; ?> <?php } ?>
-
- 2014-07-29
显示自定义分类中的自定义帖子列表
$args = array( 'tax_query' => array( array( 'taxonomy' => 'your-custom-taxonomy', 'field' => 'slug', 'terms' => array( 'your-term' ) ), ), 'post_type' => 'your-post-type' ); $loop = new WP_Query($args); if($loop->have_posts()) { $term = $wp_query->queried_object; while($loop->have_posts()) : $loop->the_post(); //Output what you want echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>'; endwhile; }
标题
- 列表项
- 列表项
- 列表项
To show a list of custom posts from a custom taxonomy
$args = array( 'tax_query' => array( array( 'taxonomy' => 'your-custom-taxonomy', 'field' => 'slug', 'terms' => array( 'your-term' ) ), ), 'post_type' => 'your-post-type' ); $loop = new WP_Query($args); if($loop->have_posts()) { $term = $wp_query->queried_object; while($loop->have_posts()) : $loop->the_post(); //Output what you want echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>'; endwhile; }
Title
- List item
- List item
- List item
有没有一种方法可以列出特定自定义帖子类型的所有帖子,并按所附的自定义分类术语进行排列?
例如;
Taxonmy Term#1
帖子类型
帖子类型
帖子类型
分类学术语#2
帖子类型
帖子类型
我们将不胜感激.
谢谢.