Wordpress 仅查询当前术语存档的直接子术语和子术语
我在我的主题中使用woocommerce,它附带了一个Wordpress 仅查询当前术语存档的直接子术语和子术语,wordpress,woocommerce,Wordpress,Woocommerce,我在我的主题中使用woocommerce,它附带了一个product\u cat术语作为商店产品的默认类别。我需要在每个产品上显示其直接子项和子项,但不显示子项 我试过: <?php $thispage = $wp_query->post; wp_list_categories("taxonomy=product_cat&term=". $term->slug."&title_li=&child_of=".$thispage->slug);
product\u cat
术语作为商店产品的默认类别。我需要在每个产品上显示其直接子项和子项,但不显示子项
我试过:
<?php $thispage = $wp_query->post; wp_list_categories("taxonomy=product_cat&term=". $term->slug."&title_li=&child_of=".$thispage->slug);?>
global $post;
$terms = get_the_terms( $post->ID, 'product_cat');
foreach ( $terms as $term )
$currentID = get_the_ID();
$args=array(
'taxonomy'=>'product_cat',
'term' => $term->slug,
'child_of'=>$currentID
);
$my_query = new WP_Query( $args );
?>
<?php if ( $my_query->have_posts() ): ?>
<?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<li> <a href="<?php the_permalink(); ?>" id="booklink"><?php the_title();?></a></li>
<?php endwhile; ?>
<?php endif; ?>
并且它返回了当前存档的所有子项和子项子项,而没有子项本身
所以我试着:
$term_id = $terms;
$taxonomy_name = 'product_cat';
$termchildren = get_term_children( $term_id, $taxonomy_name );
echo '<ul>';
foreach ( $termchildren as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
echo '<li><a href="' . get_term_link( $term->name, $taxonomy_name ) . '">' . $term->name . '</a></li>';
}
echo '</ul>';
也许我可以在一个查询args中检查它的所有子项,然后调用wp\u list\u类别。尝试:
$terms = get_the_terms( $post->ID, 'product_cat'); foreach ( $terms as $term ) $currentID = get_the_ID(); $args=array( 'taxonomy'=>'product_cat', 'term' => $term->slug, 'exclude '=> array('child_of' => get_queried_object_id()) ); $my_query = new WP_Query( $args );?>
但它只返回子项的子项,而不返回Product_猫的子项
有人吗
我说不出话来。有人有线索吗
*对不起,我的英语很差我想你可以这样做
<?php
$taxonomy = 'product_cat';
// get the term IDs assigned to post.
$post_terms = wp_get_object_terms( $product->id, $taxonomy, array( 'fields' => 'ids' ) );
// separator between links
$separator = ', ';
if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {
$term_ids = implode( ',' , $post_terms );
$terms = wp_list_categories( 'title_li=&style=none&echo=0&child_of=' .get_queried_object_id() . '&taxonomy=' . $taxonomy . '&include=' . $term_ids );
$terms = rtrim( trim( str_replace( '<br />', $separator, $terms ) ), $separator );
// display post categories
echo $terms;
}
?>
解决方案如下:
<?php
$taxonomy = 'product_cat';
// get the term IDs assigned to post.
$post_terms = wp_get_object_terms( $product->id, $taxonomy, array( 'fields' => 'ids' ) );
// separator between links
$separator = ', ';
if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {
$term_ids = implode( ',' , $post_terms );
$terms = wp_list_categories( 'title_li=&style=none&echo=0&child_of=' .get_queried_object_id() . '&taxonomy=' . $taxonomy . '&include=' . $term_ids );
$terms = rtrim( trim( str_replace( '<br />', $separator, $terms ) ), $separator );
// display post categories
echo $terms;
}
?>