如何不按字母顺序列出类别

时间:2018-10-21 09:57:26

标签: wordpress foreach wp-list-categories

您能帮我按在wordpress管理员中显示的顺序列出类别吗?

因此,我在wp页面模板中具有以下代码,其中列出了供应商(用户)的类别。

            <ul class="category-vendormenu">
            <?php foreach($categories as $category) : ?>
                <?php $liclass = ($current_cat == $category->ID) ? ' class="current"' : ''; ?>
                    <li<?php echo $liclass;?>>
                        <?php $author_posts = new WP_Query( array( 
                            'post_type' => 'product', 
                            'author' => $vendor_id, 
                            'tax_query'=>array(
                                array(
                                    'taxonomy' => 'product_cat', 
                                    'terms' => array($category->ID), 
                                    'field' => 'term_id'
                                    )
                            )
                        ));
                        $count = $author_posts->found_posts;
                        wp_reset_query();

                        ?>
                        <a href="<?php echo $store_url .'section/'. $category->ID ?>" title="<?php echo $category->name ?>">
                            <?php echo $category->name; ?>
                        </a>
                        <?php
                        if ($liclass!='') {
                        $sub_categories = $wpdb->get_results("
                            SELECT DISTINCT(terms.term_id) as ID, terms.name, terms.slug, tax.count  
                            FROM $wpdb->posts as posts
                            LEFT JOIN $wpdb->term_relationships as relationships ON posts.ID = relationships.object_ID
                            LEFT JOIN $wpdb->term_taxonomy as tax ON relationships.term_taxonomy_id = tax.term_taxonomy_id
                            LEFT JOIN $wpdb->terms as terms ON tax.term_id = terms.term_id
                            WHERE posts.post_status = 'publish' AND
                                posts.post_author = '$vendor_id' AND
                                posts.post_type = 'product' AND
                                tax.taxonomy = 'product_cat' AND
                                tax.parent='".$category->ID."' 
                            ORDER BY terms.name ASC
                        ");
                            if (count($sub_categories)>0) {
                                echo '<ul class="sub-menu">';
                                $term = get_term_by( 'id', $current_cat, 'product_cat', 'ARRAY_A' );
                                wp_register_script( 'ajax-subcategories', get_template_directory_uri() . '/js/ajax_subcategories.js', array( 'jquery' ) );
                                wp_localize_script( 'ajax-subcategories', 'parent_cat', array('ajax_url' => get_template_directory_uri() . '/functions/ajax_subcategories.php', 'store_url'=>$store_url, 'vender_id'=>$vendor_id));
                                wp_enqueue_script ( 'ajax-subcategories');
                                foreach ($sub_categories as $sub_cate) {
                                    $liclass = ($selected_sub_cat == $sub_cate->ID) ? ' class="selected"' : '';
                                    echo '<li'.$liclass.'><a href="'.$store_url .'section/'. $sub_cate->ID.'" title="'. $sub_cate->name.'" onclick="">'.$sub_cate->name.'<span>'. $sub_cate->count .'</span></a></li>';
                                }
                                echo "</ul>";
                            }
                        }
                        ?>
                    </li>
            <?php endforeach; ?>
        </ul>

我可能会大便而错过明显的东西,但是我得到的是按字母顺序排列的类别列表,这不是我想要的。

在wordpress中,类别显示如下:

  • 住宿
  • 豪华
  • 节日
  • 私人活动
  • 预定自己的

但是代码输出:

  • 住宿
  • 预定自己的
  • 节日
  • 豪华
  • 私人活动

我只是想保持在wordpress管理员中显示的顺序。

但是您可以提供帮助,谢谢!

1 个答案:

答案 0 :(得分:1)

在wp_query中,您需要使用此参数 'orderby' => 'title', 'order' => 'DESC',

您可以在本文https://codex.wordpress.org/Class_Reference/WP_Query中对此进行研究  只需搜索“ Order&Orderby参数”此段http://joxi.ru/L21dLEjc8pNkbm