wordpress 如何在WordPress中订购类别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/517940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to order categories in WordPress?
提问by Saneef
I use wp_list_categories()
to get the list of all the categories and generate the navigation bar. Is there a way to order these categories in a particular order other than alphabetical ordering.
我wp_list_categories()
用来获取所有类别的列表并生成导航栏。有没有办法按字母顺序以外的特定顺序对这些类别进行排序。
eg: Connect, News & Views, Q&A, Hello Startup, Startup 101...
例如:连接、新闻和浏览、问答、Hello Startup、Startup 101...
回答by Chasevanb
Most themes don't use the description of the category for anything. Easy workaround I did was to use numbers in description. The top post here currently has some jQuery hack from here, it's unneeded.
大多数主题不使用任何类别的描述。我所做的简单解决方法是在描述中使用数字。这里的顶级帖子目前有一些来自这里的 jQuery hack,它是不需要的。
You can add custom order fields I suppose as well.
我想您也可以添加自定义订单字段。
Just
只是
$categories = get_categories( array(
'orderby' => 'description',
'order' => 'ASC'
) );
回答by reus
If you don't use the Description field of the categories, ordering can also be done by entering a number in each Description field, and subsequently use Javascript / jQuery to order based on this numbering.
如果您不使用类别的描述字段,也可以通过在每个描述字段中输入一个数字来完成排序,然后使用 Javascript / jQuery 根据此编号进行排序。
The wp_list_categories function has a use_desc_for_titleargument which you can set to true:
wp_list_categories 函数有一个use_desc_for_title参数,您可以将其设置为 true:
wp_list_categories('use_desc_for_title=1');
The category description is now set to the <a>
tags's title attribute in the resulting html.
类别描述现在设置为<a>
结果 html 中标签的标题属性。
So if you use numbers in the description field, the resulting category listing may look like this:
因此,如果您在描述字段中使用数字,则生成的类别列表可能如下所示:
<ul id="category_list">
<li><a href="#" title="3">Category C</a></li>
<li><a href="#" title="1">Category A</a></li>
<li><a href="#" title="2">Category B</a></li>
</ul>
Now you can sort the list, for example with jQuery at the document.ready event. If you use subcategories in a hierarchical way, use a recursive function like this:
现在您可以对列表进行排序,例如在 document.ready 事件中使用 jQuery。如果您以分层方式使用子类别,请使用如下递归函数:
$( document ).ready(
function() {
sort_list_recursive( $( '#category_list' ) );
}
);
function sort_list_recursive( parent ) {
var lst = parent.children( 'li' );
lst.sort(
function( a, b ) {
var _a = a.getElementsByTagName( 'a' )[0].title;
var _b = b.getElementsByTagName( 'a' )[0].title;
return _a > _b ? 1 : -1;
}
).appendTo( parent );
lst.each( function() {
$( this ).children( 'ul' ).each( function() {
sort_list_recursive( $( this ) );
});
});
}
N.B.
NB
- Don't use a 0 (zero) in the description field, it will be ignored and set to null
- After sorting you might want to get rid of the title attributes which are set to numbers, this can easily be done with jQuery.
- 不要在描述字段中使用 0(零),它将被忽略并设置为 null
- 排序后,您可能想要摆脱设置为数字的标题属性,这可以使用 jQuery 轻松完成。
回答by Ezhil
This is inbuilt in wordpress_wp_list_categories
这是内置的 wordpress_wp_list_categories
wp_list_categories('orderby=name');
I think that would help you out
我想这会帮助你
回答by Matthias Günter
Technical approach
技术方法
The problem in wordpress core is that the table wp_terms
has no term_order
column. That means, standard wordpress does not support the custom term order. If you look at this WP database structureyou can find the table wp_term_relationships
. This table is responsible for the relationships between posts and the taxonomy (your categories) AND this table has a term_order
column.
wordpress 核心的问题是表格wp_terms
没有term_order
列。这意味着,标准 wordpress 不支持自定义术语顺序。如果您查看此WP 数据库结构,您可以找到该表wp_term_relationships
。该表负责帖子和分类法(您的类别)之间的关系,并且该表有一term_order
列。
Now, with a simple SQL statement ALTER TABLE wp_terms ADD term_order INT(11) NOT NULL DEFAULT 0
(not forget the $wpdb->wp_terms
variable) you can add a column to the table, which is responsible for your custom category order. Then you can put your custom category order in this two columns of wp_term_relationships
and wp_terms
. When all is finished, you can hook into the filter of get_terms_args
and change the orderby
to term_order
.
现在,使用简单的 SQL 语句ALTER TABLE wp_terms ADD term_order INT(11) NOT NULL DEFAULT 0
(不要忘记$wpdb->wp_terms
变量),您可以向表中添加一列,该列负责您的自定义类别顺序。然后您可以将您的自定义类别顺序放在wp_term_relationships
和 的这两列中wp_terms
。全部完成后,您可以挂钩 的过滤器get_terms_args
并将其更改orderby
为term_order
。
Here a list of all relevant links for the technical approach:
这里列出了技术方法的所有相关链接:
- https://codex.wordpress.org/Database_Descriptionfor the wp database structure
- https://codex.wordpress.org/Class_Reference/wpdbfor the
$wpdb->wp_terms
- https://developer.wordpress.org/reference/hooks/get_terms_args/for the WP filter
- https://codex.wordpress.org/Database_Description用于 wp 数据库结构
- https://codex.wordpress.org/Class_Reference/wpdb为
$wpdb->wp_terms
- https://developer.wordpress.org/reference/hooks/get_terms_args/WP 过滤器
A plugin can do the job for you
一个插件可以为您完成这项工作
Check my plugin to solve this: WordPress Real Categories Management. WP RCMcreates an extra field term_order on the wp terms table. It also brings a lot of other useful features as you can see in the screenshot below. It allows you to organize your wordpress categories in a nice way. It is easy to use, just drag&dropyour categories and move it to a specific order. The plugin works in all Custom post types.
检查我的插件来解决这个问题:WordPress Real Categories Management。WP RCM在 wp 术语表上创建一个额外的字段 term_order。它还带来了许多其他有用的功能,如下面的屏幕截图所示。它允许您以一种很好的方式组织您的 wordpress 类别。它易于使用,只需拖放您的类别并将其移动到特定顺序即可。该插件适用于所有自定义帖子类型。
From the product description i can quote. If you want to try the plugin, there is also a demo on the plugin page.
从产品描述中我可以引用。如果你想尝试插件,插件页面上还有一个演示。
There are a lot of free plugins
有很多免费插件
This can be solved with a lot of free plugins available within the wordpress.org plugin repository. Simply search for "category order" in your Wordpress Dashboard > Plugins > Install.
这可以通过 wordpress.org 插件存储库中提供的许多免费插件来解决。只需在 Wordpress 仪表板 > 插件 > 安装中搜索“类别顺序”。
回答by Andrew Patton
For the benefit of future visitors, here's the easy solution to this problem:
为了将来访问者的利益,以下是此问题的简单解决方案:
There are now a number of plugins that allow you to order categories or other custom taxonomies in WordPress. You can see some of them in the WordPress plugin directory's “category order” tag page. I can personally confirm that Custom Taxonomy Order NEplugin does the job.
现在有许多插件允许您在 WordPress 中订购类别或其他自定义分类法。您可以在 WordPress 插件目录的“类别顺序”标签页中看到其中的一些。我可以亲自确认Custom Taxonomy Order NE插件可以完成这项工作。
回答by aitor
I did it generating several term lists. I call it later by my own order. I'm a PHP beginner.
我做了它生成了几个术语列表。我稍后按我自己的命令调用它。我是一个 PHP 初学者。
First, I store, in a different variable, the ID for each category term:
首先,我将每个类别术语的 ID 存储在不同的变量中:
$terms = get_terms('my_taxonomy', 'hide_empty=0');
foreach ( $terms as $term ) {
${$term->slug} = get_term_by('slug', $term->slug, 'product_cat');
${$term->slug.'_array'} = (array)${$term->slug};
${$term->slug.'_array_id'} =${$term->slug.'_array'}['term_id'];
};
Then, I create several args for each wp_list_categories()
excluding, with this variable the terms I want to:
然后,我为每个wp_list_categories()
排除项创建了几个 args ,这个变量是我想要的术语:
$args = array(
'taxonomy' => 'my_taxonomy',
'orderby' => 'name',
'show_count' => true,
'pad_counts' => false,
'hierarchical' => true,
'title_li' => '',
'hide_empty' => 0,
'show_option_all' => 'Show all',
'exclude' => array( $term1_array_id, $term2_array_id )
);
$args_1 = array(
'taxonomy' => 'my_taxonomy',
'orderby' => 'name',
'show_count' => true,
'pad_counts' => false,
'hierarchical' => true,
'title_li' => '',
'hide_empty' => 0,
'exclude' => array( $term3_array_id, $term4_array_id, $term1_array_id )
);
$args_2 = array(
'taxonomy' => 'my_taxonomy',
'orderby' => 'name',
'show_count' => true,
'pad_counts' => false,
'hierarchical' => true,
'title_li' => '',
'hide_empty' => 0,
'exclude' => array( $term1_array_id, $term4_array_id, $term5_array_id )
);
Finally, I can call separately each term list:
最后,我可以分别调用每个术语列表:
<ul>
<?php wp_list_categories( $args ); ?>
<?php wp_list_categories( $args_1 ); ?>
<?php wp_list_categories( $args_2 ); ?>
</ul>