繁体   English   中英

在父分类页面上按自定义分类孩子检索帖子

[英]Retrieve posts by custom taxonomy child on parent taxonomy page

因此,从父分类法的角度来看:

  • 儿童1

    • 岗位
    • 岗位
    • 岗位
  • 儿童2

    • 岗位
    • 岗位
    • 岗位
    • 岗位
  • 儿童3

    • 岗位
    • 岗位
    • 岗位
    • 岗位

我已经在互联网上搜寻了解决方案,但是似乎没有任何效果。 我能够成功回显术语ID,但是当我将其传递给查询时,它不会返回任何内容。

 <?php $terms = get_terms( 'ht_kb_category' ); if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){ echo '<ul>'; foreach ( $terms as $term ) { if ($term->parent != 0) { echo '<li><h1>' . $term->name . '</h1></li>'; $the_query = new WP_Query( array( 'post_type' => 'post', 'tax_query' => array( array ( 'taxonomy' => 'ht_kb_category', 'field' => 'slug', 'terms' => $term->slug, ) ), ) ); while ( $the_query->have_posts() ) : echo '<p>'. the_title() .'</p>'; endwhile; /* Restore original Post Data * NB: Because we are using new WP_Query we aren't stomping on the * original $wp_query and it does not need to be reset. */ wp_reset_postdata(); } } echo '</ul>'; } ?> 

经过一些使用自定义分类法URL的侦探工作,终于找到了解决方案。 我错过了“ post_type”,需要查询“ tag_id”而不是“ term_id”。

 <?php $current_term_id = $hkb_current_term_id ?> <?php $myposts = get_posts(array( 'showposts' => -1, 'post_type' => 'ht_kb', 'tax_query' => array( array( 'taxonomy' => 'ht_kb_category', 'field' => 'tag_id', 'terms' => $current_term_id) )) ); echo '<ul>'; foreach ($myposts as $mypost) { ?> <li><a href="<?php echo get_permalink($mypost->ID) ?>"><?php echo $mypost->post_title ?></a></li> <?php } echo '</ul>'; ?> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM