如何在类别中添加自定义元字段?
有谁知道如何在创建类别时添加自定义元字段并在 WordPress 的循环中获取它们?我想知道如何在不破解 WordPress 核心的情况下做到这一点,但如果我这样做 - 它不会成为将来更新 WordPress 的障碍。
我发现一个接近的插件是 Wp-Category-Meta,但它无法将复选框添加为编辑类别中的字段。
这将非常有用,因为用户可以将某些类别设为“特色”,然后代码可以在中使用该元值以不同方式设置“特色”类别样式的循环。
Does anyone have any idea how to add custom meta fields while making categories and fetch them in the loop in WordPress? I was wondering how to do that without hacking the WordPress core, but if I do – it won't become a hindrance to update WordPress in the future.
A plugin I have found that comes close is Wp-Category-Meta, but it doesn't have the ability to add checkboxes as fields in Edit Categories.
This will be very useful as users can make certain categories "featured", and then the code can use that meta value in the loop to style "featured" categories differently.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题:Wordpress 没有存储分类法“元”值的结构或方法。
2017 年更新:WP 4.4+ 有“术语元”!
要使用术语元,请使用以下命令:
update_term_meta()
get_term_meta()
delete_term_meta()
add_term_meta()
以下操作仍然有效! :)
附加阅读:4.4 分类法Roundup
WP 版本 <= 4.3.x 和常见操作的解决方案
操作:
create_category
和edit_category
类别 editcategory_add_form_fields
和category_edit_form
对于类别表单字段操作比我介绍的要多,但它们似乎已弃用(根据developer.wordpress.org)。
我选择这些行动的原因:
- 他们在 WordPress 4.4.2 上运行
- 由于缺乏文档,我认为这些是新的,取代了已弃用的......
功能:
get_option( $option, $default );
update_option( $option, $new_value, $autoload );
update_option
有两个强大的功能:a) 当该选项尚不存在时,它会创建该选项
b)
$new_value
可以是整数、字符串、数组或对象。您可能会问,为什么要使用数组/对象? ...好吧,因为每个选项 = 1 个数据库行 =>您可能希望将类别选项存储在一行行中:)
代码
创建或编辑?
您可能想知道您是否正在创建或保存一个类别 - 这还没有记录(据我所知),但来自测试:
$tag_object
是object
并包含一些属性,最值得注意的是:term_id
分类
过滤器
$tag_object
只是一个常规的string
“类别” - 我想这将来可能会改变...一般分类法
一般分类法也有类似的操作 - 检查这些操作。
The problem:Wordpress does not have a structure nor method to store "meta" values for taxonomies.
UPDATE 2017: WP 4.4+ has "term meta"!
For working with term metas use these:
update_term_meta()
get_term_meta()
delete_term_meta()
add_term_meta()
The Actions below are still valid though! :)
Additional reading: 4.4 Taxonomy Roundup
Solution for WP version <= 4.3.x and COMMON actions
Actions:
create_category
andedit_category
for category editcategory_add_form_fields
andcategory_edit_form
for category form fieldsThere are more actions than I've presented, but they seem to be deprecated (according to developer.wordpress.org).
The reason I chose the actions that I chose:
- They work on WordPress 4.4.2
- Due to lack of documentation I assumed these are the new ones replacing the deprecated ones...
Functions:
get_option( $option, $default );
update_option( $option, $new_value, $autoload );
update_option
has two great abilities:a) It craetes the option when such option does not exist yet
b)
$new_value
can be an integer, string, array, or object.You may ask, why to use array/object? ...well, because each option = 1 database row => you probably want to store your category options in one row :)
The CODE
Create or Edit?
You might wonder whether you are creating or saving a category - this not documented yet (as far as I know), but from testing:
$tag_object
isobject
and contains some properties, most notably:term_id
taxonomy
filter
$tag_object
is just a regularstring
"category" - I guess this might change in the future...General taxonomy
There are also actions like these for taxonomies in general - check these actions.
贾兹,
看起来您在原始问题中提到的插件已更新为包含复选框字段(包含在 v1.2.3 中)
Jaz,
It looks like the plugin you mention in your original question has been updated to include a checkbox field (included in v1.2.3)
我认为 类别 SEO 元标签 插件会对您有所帮助。
I think the Category SEO Meta Tags plugin will help you.
可以在这里找到该插件的更新和重构版本:
https ://wordpress.org/plugins/custom-taxonomy-category-and-term-fields/
还添加了所见即所得编辑器字段类型。
There is an updated and refactured version of this plugin to be found here:
https://wordpress.org/plugins/custom-taxonomy-category-and-term-fields/
Also added a WYSIWYG editor fieldtype.