### [如何为WordPress插件添加文章分类管理功能](https://www.prosaas.cn/article/2405) **Published:** 2023-09-05T00:43:42 **Author:** 5201314 **Excerpt:** 如何为WordPress插件添加文章分类管理功能WordPress是目前使用最广泛的内容管理系统之一,它提供了… ![](//zhiqipro.oss-cn-beijing.aliyuncs.com/2023/12/20231212120843668.jpg) 如何为WordPress插件添加文章分类管理功能 WordPress是目前使用最广泛的内容管理系统之一,它提供了丰富的插件来扩展其功能。如果你是一个插件开发者,可能会遇到需要为你的插件添加文章分类管理功能的需求。本文将为你介绍如何为WordPress插件添加文章分类管理功能,并提供代码示例供参考。 1. 创建分类 首先,我们需要为插件创建一个新的文章分类。可以通过使用`register_taxonomy()`函数来完成这一任务。以下是一个示例代码: // 在插件的主活动文件中添加以下代码 function custom\_plugin\_taxonomy() { $labels = array( 'name' => \_x( '插件分类', 'taxonomy general name', 'textdomain' ), 'singular\_name' => \_x( '插件分类', 'taxonomy singular name', 'textdomain' ), 'search\_items' => \_\_( '搜索分类', 'textdomain' ), 'all\_items' => \_\_( '所有分类', 'textdomain' ), 'parent\_item' => \_\_( '父级分类', 'textdomain' ), 'parent\_item\_colon' => \_\_( '父级分类:', 'textdomain' ), 'edit\_item' => \_\_( '编辑分类', 'textdomain' ), 'update\_item' => \_\_( '更新分类', 'textdomain' ), 'add\_new\_item' => \_\_( '添加新分类', 'textdomain' ), 'new\_item\_name' => \_\_( '新分类名称', 'textdomain' ), 'menu\_name' => \_\_( '分类', 'textdomain' ), ); $args = array( 'hierarchical' => true, 'labels' => $labels, 'show\_ui' => true, 'show\_admin\_column' => true, 'query\_var' => true, 'rewrite' => array( 'slug' => 'plugin\_category' ), ); register\_taxonomy( 'plugin\_category', array( 'post' ), $args ); } add\_action( 'init', 'custom\_plugin\_taxonomy', 0 ); 在上面的代码中,我们使用`register_taxonomy()`函数来创建一个名为`plugin_category`的新文章分类。这个分类具有一些基本属性,例如名称、搜索文本和编辑操作等。 2. 为插件启用分类管理 现在,我们需要在插件中添加一个界面,让用户可以在文章编辑页面中选择和管理分类。我们可以使用钩子函数`add_meta_box()`来实现这一点。以下是一个示例代码: // 在插件的主活动文件中添加以下代码 function custom\_plugin\_taxonomy\_meta\_box() { add\_meta\_box( 'plugin\_category', \_\_( '插件分类', 'textdomain' ), 'custom\_plugin\_taxonomy\_meta\_box\_callback', 'post', 'side', 'default' ); } add\_action( 'add\_meta\_boxes', 'custom\_plugin\_taxonomy\_meta\_box' ); function custom\_plugin\_taxonomy\_meta\_box\_callback( $post ) { wp\_nonce\_field( 'custom\_plugin\_taxonomy\_meta\_box', 'custom\_plugin\_taxonomy\_meta\_box\_nonce' ); $terms = get\_terms( array( 'taxonomy' => 'plugin\_category', 'hide\_empty' => false, ) ); if ( ! empty( $terms ) && ! is\_wp\_error( $terms ) ) { echo ''; } } 在上面的代码中,我们使用`add_meta_box()`函数添加一个新的元框,用于显示分类选择框。在`custom_plugin_taxonomy_meta_box_callback()`函数中,我们使用`get_terms()`函数获取所有可用的分类,并输出一个下拉菜单供用户选择。 3. 保存和更新分类 最后,我们需要添加代码来保存和更新所选的分类。我们可以使用钩子函数`save_post`来处理这个任务。以下是一个示例代码: // 在插件的主活动文件中添加以下代码 function custom\_plugin\_taxonomy\_save\_meta\_box\_data( $post\_id ) { if ( ! isset( $\_POST\['plugin\_category'\] ) ) { return; } if ( ! wp\_verify\_nonce( $\_POST\['custom\_plugin\_taxonomy\_meta\_box\_nonce'\], 'custom\_plugin\_taxonomy\_meta\_box' ) ) { return; } if ( defined( 'DOING\_AUTOSAVE' ) && DOING\_AUTOSAVE ) { return; } if ( ! current\_user\_can( 'edit\_post', $post\_id ) ) { return; } $term\_id = intval( $\_POST\['plugin\_category'\] ); wp\_set\_post\_terms( $post\_id, array( $term\_id ), 'plugin\_category' ); } add\_action( 'save\_post', 'custom\_plugin\_taxonomy\_save\_meta\_box\_data' ); 在上面的代码中,我们检查了分类选择框是否被选中,并使用`wp_verify_nonce()`函数验证提交的表单数据。然后,我们使用`wp_set_post_terms()`函数将所选分类保存到当前文章中。 通过以上步骤,你已经成功为你的WordPress插件添加了文章分类管理功能。用户现在可以在文章编辑页面中选择和管理分类,以实现更好的内容管理体验。 总结 本文介绍了如何为WordPress插件添加文章分类管理功能,并提供了相关代码示例。通过使用`register_taxonomy()`函数创建分类,使用`add_meta_box()`函数添加界面,以及使用`save_post`钩子函数保存和更新分类,你可以将这一功能快速地集成到自己的插件中。希望本文对你有所帮助,祝你编写出更加强大的WordPress插件! 以上就是如何为WordPress插件添加文章分类管理功能的详细内容,更多请关注知企PROSAAS其它相关文章! **Tags:** WordPress **Categories:** WordPress ---