WordPress获得条款和命令

WordPress获得条款和命令

我正在使用以下代码在自定义分类法中查询术语:

<?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>';

  }
?>

一切都很好!我的问题是我想按菜单顺序或类似顺序进行订购。目前,它们处于随机顺序!

谁能建议我需要做什么?

干杯丹

最佳答案

尝试使用get_terms函数。您的代码将不是这样(您可以指定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>';
          }
      }
  }
?>

关于wordpress - WordPress获得条款和命令,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14560108/

10-10 04:10