WordPress插件开发:自定义分类方法

2025-10-24 303
WordPress

类型:CMS系统

简介:一款开源的内容管理系统(CMS),用于构建和管理网站。

WordPress插件开发中自定义分类方法即自定义类别和分组,具有分级和扁平的特点,分类法含有一些分类法项目,一般存储在wp_terms的数据表中,而分类法本身则存储在term_taxonomy数据表中。下文将由站长百科详细分享在WordPress插件开发中注册自定义分类方法的方法,包括从数据库中获取自定义分类方法内容,并将内容呈现给用户。

一、使用自定义分类法

虽然WordPress本身自带 “分类目录” 和 “标签” ,但是大部分时候已经无法满足我们的需求,这时候分类法系统的推出恰巧满足了这个“缺口”,支持自定义分类方法,一般和自定义文章类型一起使用,关于自定义文章类型的使用方法会在下期为大家详细介绍。

至于使用自定义分类法的优势,无疑就是可以使用独立于“分类目录” 和 “标签”的分类系统来区分我们的目标内容。例如我们需要帮一个服装店店主搭建一个WordPress线上商店,为了避免“秋装”和其他内容混在一起,我们需要创建一个自定义文章类型“秋装”来存放相应的服装类型。而“秋装”又分为“大衣”、“休闲裤”、“羊毛衫”等等,就需要创建不同的分类方法把这些产品分开,如此一来是不是更好理解?

为方便我们管理,WordPress自定义分类方法在厚外有自己的独立菜单,另外创建自定义分类方法还支持创建自定义界面和数据出入流程,可以显著降低学习和使用成本。若在插件中创建了自定义分类方法,还可以在不同的WordPress网站上循环使用。

下面例举了一个简单的示例,展示创建自定义分类法“课程”的方法,并添加到默认“文章”类型中。在实操以下教程前,请先阅读:《WordPress插件开发入门》。

首先打开“添加文章”页面,可以看到默认有分类目录和标签这两种分类方法。通过init Action钩子将内为内容类型 “文章” 注册一个名为 “课程”的自定义分类方法 。

/*
* Plugin Name: Course Taxonomy
* Description: A short example showing how to add a taxonomy called Course.
* Version: 1.0
* Author: developer.wordpress.org
* Author URI: https://codex.wordpress.org/User:Aternus
*/

function wporg_register_taxonomy_course() {
$labels = [
'name' => _x( 'Courses', 'taxonomy general name' ),
'singular_name' => _x( 'Course', 'taxonomy singular name' ),
'search_items' => __( 'Search Courses' ),
'all_items' => __( 'All Courses' ),
'parent_item' => __( 'Parent Course' ),
'parent_item_colon' => __( 'Parent Course:' ),
'edit_item' => __( 'Edit Course' ),
'update_item' => __( 'Update Course' ),
'add_new_item' => __( 'Add New Course' ),
'new_item_name' => __( 'New Course Name' ),
'menu_name' => __( 'Course' ),
];
$args = [
'hierarchical' => true, // make it hierarchical (like categories)
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => [ 'slug' => 'course' ],
];
register_taxonomy( 'course', [ 'post' ], $args );
}

add_action( 'init', 'wporg_register_taxonomy_course' );

代码解读:

  • wporg_register_taxonomy_course:注册自定义文章类型所需要的所有步骤;
  • $label:定义了自定义分类方法的标签,即后台的分类方法的名称、操作等各种信息;
  • $args:包含创建自定义分类方法时使用的配置选项,用来指示WordPress设置自定义分类方法;
  • register_taxonomy():用$args数组为“文章” 这个内容类型配置了课程分类方法;
  • add_action():将创建分类方法的操作绑定到了init Action 钩子上。

激活刚创建的WordPress插件,再次打开“新建文章”页面,将看到一个名为“课程”的分类方法。

WordPress插件开发:自定义分类方法

另外WordPress中还有很多函数可以操作自定义分类法和自定义分类法中的分类项目。例如:

  • the_terms:接受分类法参数,在列表中显示分类法项目;
  • wp_tag_cloud:接受分类法参数,以标签云的形式显示分类法项目;
  • is_taxonomy:判断是否为指定的分类方法。

二、拆分分类法项目

自WordPress 4.2以后,不同分类方法中使用的相同分类法项目时会被拆分为单独的项目,并且会将每个项目会被分配一个新的ID。还有一些插件和主题会把一些分类法ID存储到options, post_meta, user_meta或其他数据中,如此一来主题和插件就很可能会受到影响。

WordPress 4.2之后提供了两个不通的工具来帮助插件和主题开发者进行转换:

1、split_shared_term钩子

若共享的分类法项目被分配了新的ID,就会自动触发挂载到split_share_term钩子上的操作。下面演示主题和插件开发者使用此钩子确保存储的分类法项目ID被更新的操作:

存储于选项中:

/**
* Update featured_tags option when a shared term gets split.
*
* @param int $term_id ID of the formerly shared term.
* @param int $new_term_id ID of the new term created for the $term_taxonomy_id.
* @param int $term_taxonomy_id ID for the term_taxonomy row affected by the split.
* @param string $taxonomy Taxonomy for the split term.
*/
function wporg_featured_tags_split( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {
// we only care about tags, so we'll first verify that the taxonomy is post_tag.
if ( $taxonomy === 'post_tag' ) {

// get the currently featured tags.
$featured_tags = get_option( 'featured_tags' );

// if the updated term is in the array, note the array key.
$found_term = array_search( $term_id, $featured_tags );
if ( $found_term !== false ) {

// the updated term is a featured tag! replace it in the array, save the new array.
$featured_tags[ $found_term ] = $new_term_id;
update_option( 'featured_tags', $featured_tags );
}
}
}

add_action( 'split_shared_term', 'wporg_featured_tags_split', 10, 4 );

以上示例假设插件存储了“feartured_tags”选项,包含了一个标签ID数组,该选项为主页精选文章模块的查询参数。挂载一个函数到split_shared_term Action钩子上,会检查标签ID是否在数组中,如果有需要会更新这个数组。

存储于post_meta中:

/**
* Update related posts term ID for pages
*
* @param int $term_id ID of the formerly shared term.
* @param int $new_term_id ID of the new term created for the $term_taxonomy_id.
* @param int $term_taxonomy_id ID for the term_taxonomy row affected by the split.
* @param string $taxonomy Taxonomy for the split term.
*/
function wporg_page_related_posts_split( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {
// find all the pages where meta_value matches the old term ID.
$page_ids = get_posts( [
'post_type' => 'page',
'fields' => 'ids',
'meta_key' => 'meta_key',
'meta_value' => $term_id,
] );

// if such pages exist, update the term ID for each page.
if ( $page_ids ) {
foreach ( $page_ids as $id ) {
update_post_meta( $id, 'meta_key', $new_term_id, $term_id );
}
}
}

add_action( 'split_shared_term', 'wporg_page_related_posts_split', 10, 4 );

若插件为页面保存了一个标签ID,那么通过这个标签ID我们可以在文章页面中显示相关文章。先通过 get_psots() 函数来获取 meta_key 和 meta_value 匹配的页面,然后更新 meta_value 为分割后的 ID。

2、wp_get_split_term钩子

wp_get_split_term函数主要作用是处理分类法项目ID的更改,缺点是可能会出现插件没有机会执行split_shared_term钩子的情况。以下实例中插件存储了一个名为feartured_tags,值为分类法项目 ID 的数组选项,可能需要创建一个函数来检查这些 ID(可以在插件更新的时候)中是否包含已被拆分的 ID,如果包含,则需要更新被拆分后分类法项目 ID 到这个数组中。

需要注意但是,wp_get_split_term()接受$old_term_id和$taxonomy ,并返回一个整数。如果我们需要获取所有分类法中与旧分类法ID相关的拆分分类法项目列表,可以使用wp_get_split_terms() 函数。

function wporg_featured_tags_check_split() {
$featured_tag_ids = get_option( 'featured_tags', [] );

// check to see whether any IDs correspond to post_tag terms that have been split.
foreach ( $featured_tag_ids as $index => $featured_tag_id ) {
$new_term_id = wp_get_split_term( $featured_tag_id, 'post_tag' );

if ( $new_term_id ) {
$featured_tag_ids[ $index ] = $new_term_id;
}
}

// save
update_option( 'featured_tags', $featured_tag_ids );
}
  • 广告合作

  • QQ群号:4114653

温馨提示:
1、本网站发布的内容(图片、视频和文字)以原创、转载和分享网络内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。邮箱:2942802716#qq.com(#改为@)。 2、本站原创内容未经允许不得转裁,转载请注明出处“站长百科”和原文地址。