如何在类别中添加自定义元字段?

发布于 2024-10-01 08:47:44 字数 404 浏览 8 评论 0原文

有谁知道如何在创建类别时添加自定义元字段并在 WordPress 的循环中获取它们?我想知道如何在不破解 WordPress 核心的情况下做到这一点,但如果我这样做 - 它不会成为将来更新 WordPress 的障碍。

我发现一个接近的插件是 Wp-Category-Meta,但它无法将复选框添加为编辑类别中的字段。

screenshot

这将非常有用,因为用户可以将某些类别设为“特色”,然后代码可以在中使用该元值以不同方式设置“特色”类别样式的循环。

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.

screenshot

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜心 2024-10-08 08:47:44

问题:
Wordpress 没有存储分类法“元”值的结构或方法。

2017 年更新:WP 4.4+ 有“术语元”!

要使用术语元,请使用以下命令:

update_term_meta()

get_term_meta()

delete_term_meta()

add_term_meta()

以下操作仍然有效! :)

附加阅读:4.4 分类法Roundup

WP 版本 <= 4.3.x 和常见操作的解决方案

操作:

  1. create_categoryedit_category 类别 edit
  2. category_add_form_fieldscategory_edit_form 对于类别表单字段

操作比我介绍的要多,但它们似乎已弃用(根据developer.wordpress.org)。

我选择这些行动的原因:

- 他们在 WordPress 4.4.2 上运行

- 由于缺乏文档,我认为这些是新的,取代了已弃用的......

功能:

  1. get_option( $option, $default );
  2. update_option( $option, $new_value, $autoload );

update_option 有两个强大的功能:

a) 当该选项尚不存在时,它会创建该选项

除非需要指定add_option()的可选参数,
update_option() 对于添加和更新来说是一个有用的包罗万象的方法
选项。

b) $new_value 可以是整数、字符串、数组或对象。

您可能会问,为什么要使用数组/对象? ...好吧,因为每个选项 = 1 个数据库行 =>您可能希望将类别选项存储在一行行中:)

代码

  function my_category_form_fields($tag_object){
    //output/display extra form fields, e.g. by echo ...
    //ADD EXTRA SPECIFIC FIELD TO LATER CHECK IF IT'S CATEGORY SAVE/EDIT!
    //(see note at 'edit_category' action...)

    if( !empty($tag_object['term_id']) ){
      //edit category form specific
      //...load existing options with get_option( $option, $default );
    } else {
      //create category form specific
    }
  }

  function my_category_save(){
    //CHECK FOR YOUR EXTRA SPECIFIC FIELD TO CHECK IF IT'S CATEGORY SAVE/EDIT
    //(see note at 'edit_category' action...)

    //SECURITY CHECK
    if( empty($_POST['EXTRA_SPECIFIC_FIELD']) || ! current_user_can('manage_categories') )
       return null;

    //save your form values using update_option()
    //Recommendation:
    //Add "category_" prefix and $category_id to your option name!
  }

  add_action( 'create_category', 'my_category_save', 10, 1 ); 

  //Runs when a category is updated/edited,
  //INCLUDING when a post or blogroll link is added/deleted or its categories are updated
  //(which causes the count for the category to update)
  add_action( 'edit_category',   'my_category_save', 10, 1 ); 

  add_action( 'category_add_form_fields', 'my_category_form_fields', 10, 1 ); 
  add_action( 'category_edit_form',       'my_category_form_fields', 10, 1 ); 

创建或编辑?

您可能想知道您是否正在创建或保存一个类别 - 这还没有记录(据我所知),但来自测试:

  1. 编辑保存=> $tag_objectobject 并包含一些属性,最值得注意的是:
    • term_id
    • 分类
    • 过滤器
  2. 创建保存=> $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:

  1. create_category and edit_category for category edit
  2. category_add_form_fields and category_edit_form for category form fields

There 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:

  1. get_option( $option, $default );
  2. update_option( $option, $new_value, $autoload );

update_option has two great abilities:

a) It craetes the option when such option does not exist yet

Unless you need to specify the optional arguments of add_option(),
update_option() is a useful catch-all for both adding and updating
options.

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

  function my_category_form_fields($tag_object){
    //output/display extra form fields, e.g. by echo ...
    //ADD EXTRA SPECIFIC FIELD TO LATER CHECK IF IT'S CATEGORY SAVE/EDIT!
    //(see note at 'edit_category' action...)

    if( !empty($tag_object['term_id']) ){
      //edit category form specific
      //...load existing options with get_option( $option, $default );
    } else {
      //create category form specific
    }
  }

  function my_category_save(){
    //CHECK FOR YOUR EXTRA SPECIFIC FIELD TO CHECK IF IT'S CATEGORY SAVE/EDIT
    //(see note at 'edit_category' action...)

    //SECURITY CHECK
    if( empty($_POST['EXTRA_SPECIFIC_FIELD']) || ! current_user_can('manage_categories') )
       return null;

    //save your form values using update_option()
    //Recommendation:
    //Add "category_" prefix and $category_id to your option name!
  }

  add_action( 'create_category', 'my_category_save', 10, 1 ); 

  //Runs when a category is updated/edited,
  //INCLUDING when a post or blogroll link is added/deleted or its categories are updated
  //(which causes the count for the category to update)
  add_action( 'edit_category',   'my_category_save', 10, 1 ); 

  add_action( 'category_add_form_fields', 'my_category_form_fields', 10, 1 ); 
  add_action( 'category_edit_form',       'my_category_form_fields', 10, 1 ); 

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:

  1. Edit save => $tag_object is object and contains some properties, most notably:
    • term_id
    • taxonomy
    • filter
  2. Create save => $tag_object is just a regular string "category" - I guess this might change in the future...

General taxonomy

There are also actions like these for taxonomies in general - check these actions.

小镇女孩 2024-10-08 08:47:44

贾兹,
看起来您在原始问题中提到的插件已更新为包含复选框字段(包含在 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)

Bonjour°[大白 2024-10-08 08:47:44

我认为 类别 SEO 元标签 插件会对您有所帮助。

I think the Category SEO Meta Tags plugin will help you.

猫腻 2024-10-08 08:47:44

可以在这里找到该插件的更新和重构版本:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文