[英]WordPress get terms and orderby
I am querying the terms in a custom taxonomy i have with the following code:我正在使用以下代码查询自定义分类中的术语:
<?php
$term_slug = get_query_var('term');
$taxonomyName = get_query_var('taxonomy');
$current_term = get_term_by('slug', $term_slug, $taxonomyName);
$termchildren = get_term_children($current_term->term_id, $taxonomyName);
foreach ($termchildren as $child) {
$term = get_term_by('id', $child, $taxonomyName);
echo '<li id="mid"><a href="#tab-' . $slug = $term->slug . '">' . $term->name . '</a></li>';
}
?>
Try to use get_terms function. 尝试使用get_terms函数。 Your code than would be like this (you can specifiy orderby and order arg)
你的代码比这样(你可以指定orderby和order arg)
<?php
$term_slug = get_query_var('term');
$taxonomy_name = get_query_var('taxonomy');
$current_term = get_term_by('slug', $term_slug, $taxonomy_name);
if ( true === is_a( $current_term, 'WP_Term' ) ) {
$args = array(
'child_of' => $current_term->term_id,
'orderby' => 'id',
'order' => 'DESC'
);
$terms = get_terms($taxonomyName, $args);
if ( true === is_array( $terms ) ) {
foreach ($terms as $term) {
echo '<li id="mid"><a href="#tab-' . esc_attr( $term->slug ) . '">' . esc_html( $term->name ) . '</a></li>';
}
}
}
?>
Use the code in the taxonomy file:使用分类文件中的代码:
$wterm = get_queried_object();
$termslug = $wterm->slug;
$termname = $wterm->name;
$termid = $wterm->term_id;
$terms = get_terms( 'taxonomy-name', array(
'orderby' => 'name',
'hide_empty' => 0,
'child_of' => $termid
) );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
echo $term->name ;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.