如何列出不按字母顺序排列的类别
How to list categories NOT in alphabetical order
你能帮我按照它们在 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 管理中显示的顺序。
无论您如何帮助,谢谢!
在wp_query中你需要使用这个参数
'orderby' => 'title',
'order' => 'DESC',
你可以在这篇文章中研究这个https://codex.wordpress.org/Class_Reference/WP_Query
只需搜索 "Order & Orderby Parameters" 这个片段 http://joxi.ru/L21dLEjc8pNkbm
你能帮我按照它们在 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 管理中显示的顺序。
无论您如何帮助,谢谢!
在wp_query中你需要使用这个参数
'orderby' => 'title',
'order' => 'DESC',
你可以在这篇文章中研究这个https://codex.wordpress.org/Class_Reference/WP_Query 只需搜索 "Order & Orderby Parameters" 这个片段 http://joxi.ru/L21dLEjc8pNkbm