建站教程

建站教程

Products

当前位置:首页 > 建站教程 >

WordPress 添加自定义代码(WordPress插入分类法函数:wp_insert_term)

GG网络技术分享 2025-03-18 16:13 9


WordPress 添加自定义代码

1. 问题描述

在 WordPress 中,我们能够添加自定义代码,这能够通过修改主题文件实现。但是如果主题升级,那么我们的修改将会丢失。因此,通过修改主题文件来添加自定义代码并不是个好主意。

好在有相关的插件解决类似问题,比如 Insert Headers and Footers(https://wordpress.org/plugins/insert-headers-and-footers/) 插件,这也是我们将要介绍的插件(类似的插件有很多,我们选择当前下载量和排名比较靠前的同类插件)。

该笔记将记录:在 WorkPress 中,如何使用 Insert Headers and Footers(https://wordpress.org/plugins/insert-headers-and-footers/) 插件来添加自定义代码,以及相关问题处理。

2. 解决方案

2.1. 第一步、安装插件

安装插件的方法不再赘述,直接在管理后台搜索并安装 Insert Headers and Footers(https://wordpress.org/plugins/insert-headers-and-footers/) 插件即可。

2.2. 第二步、添加自定义代码

在管理后台中,访问 Settings » Insert Headers and Footers 页面,然后添加自定义代码。

在添加完成后,点击 Save 保存,并刷新页面查看效果。

3. 参考文献

How to Add Header and Footer Code in WordPress (the Easy Way)(https://www.wpbeginner.com/plugins/how-to-add-header-and-footer-code-in-wordpress/ )


WordPress插入分类法函数:wp_insert_term

WordPress函数wp_insert_term用于插入一个分类法项目

wp_insert_term( string $term, string $taxonomy, array|string $args = array() )

函数参数

$term

字符串

分类名称

$taxonomy

字符串

分类法名称

$args

数组或字符串

分类的参数,可用值如下:

  • alias_of:别名;
  • description:分类的简要描述;
  • parent:如果指定分类法支持层级,可以对创建的分类指定父分类;
  • slug:别名。

函数返回值

插入分类成功返回包含term_id和term_taxonomy_id的数组,失败返回WP_Error

函数使用示例

wp_insert_term(\'图书\', \'product\', 

array(

\'description\' => \'图书分类描述\',

\'slu\' => \'books\',

\'parent\' => 1,

));

扩展阅读

wp_insert_term()函数位于:wp-includes/taxonomy.php

相关函数:

  • wp_insert_term_data()
  • edit_terms()

标签:

提交需求或反馈

Demand feedback