wordpress添加文章固定字段的介绍
这篇文章主要介绍了关于wordpress添加文章固定字段的介绍,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
让wordpress的文章数据表 增加一个字段,使其能在文章编辑页能编辑,并能通过rest api 获取出来。
例:给文章加一个缩略图字段 litpic
首先 通过mysql 给文章表 wp_posts 加一个字段 litpic
然后在主题的function.php 后面添加如下代码:
add_action( 'add_meta_boxes', 'myplugin_add_custom_box'); add_action( 'save_post', 'myplugin_save_postdata');function myplugin_add_custom_box() { add_meta_box('myplugin_sectionid', '设置缩略图', // 可自行修改标题文字 'myplugin_inner_custom_box', 'post'); }function myplugin_inner_custom_box( $post ) { global $wpdb; // Use nonce for verification wp_nonce_field( plugin_basename( __FILE__ ), 'myplugin_noncename' ); // 获取固定字段litpic的值,用于显示之前保存的值 // 此处wp_posts新添加的字段为litpic,多个用半角逗号隔开 $date = $wpdb->get_row( $wpdb->prepare( "SELECT litpic FROM $wpdb->posts WHERE ID = %d", $post->ID) ); // litpic 字段输入框的HTML代码 echo '<label for="litpic_new_field">图片url </label>'; echo '<input type="text" id="litpic_new_field" name="litpic_new_field" value="'.$date->litpic.'" size="28" />'; // 多个字段依此类推 } function myplugin_save_postdata( $post_id ) { // verify if this is an auto save routine. // If it is our form has not been submitted, so we dont want to do anything if ( defined( ’DOING_AUTOSAVE’ ) && DOING_AUTOSAVE )return; // verify this came from the our screen and with proper authorization, // because save_post can be triggered at other times if ( !wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename( __FILE__ ) ) )return; // 权限验证 if ( 'post' == $_POST['post_type'] ) { if ( !current_user_can( 'edit_post', $post_id ) )return; } // 获取编写文章时填写的固定字段的值,多个字段依此类推 $litpic = $_POST['litpic_new_field'];global $wpdb;$wpdb->update( "$wpdb->posts", // 以下一行代码,多个字段的话参照下面的写法,单引号中是字段名,右边是变量值。半角逗号隔开 array( 'litpic' => $litpic), array( 'ID' => $post_id ), // 添加了多少个新字段就写多少个%s,半角逗号隔开 array( '%s'), array( '%d') ); }
添加后,文章页会显示litpic字段的输入框,如图:
但此时 rest api还不会把litpic字段输出。
打开 /wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php 文件。
添加如下代码:
if ( ! empty( $schema['properties']['litpic'] ) ) { $data['litpic'] = $post->litpic; }
'litpic' => array( 'description' => __( 'A litpic to protect access to the content and excerpt.' ), 'type' => 'string', 'context' => array( 'view', 'edit', 'embed' ), ),
$post_type_attributes = array( 'title', 'editor', 'author', 'excerpt', 'thumbnail', 'comments', 'revisions', 'page-attributes', 'post-formats', 'custom-fields', 'litpic', ); $fixed_schemas = array( 'post' => array( 'title', 'editor', 'author', 'excerpt', 'thumbnail', 'comments', 'revisions', 'post-formats', 'custom-fields', 'litpic', ),
case 'litpic': $schema['properties']['litpic'] = array( 'description' => __( 'The ID for the litpic of the object.' ), 'type' => 'string', 'context' => array( 'view', 'edit', 'embed' ), ); break;
现在,rest api 就可以把litpic 字段输出了。
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
以上是wordpress添加文章固定字段的介绍的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

PHP和Flutter是移动端开发的流行技术。Flutter胜在跨平台能力、性能和用户界面,适合需要高性能、跨平台和自定义UI的应用程序。PHP则适用于性能较低、不跨平台的服务器端应用程序。

通过编辑 style.css 文件,您可以轻松修改 WordPress 页面宽度:编辑 style.css 文件,添加 .site-content { max-width: [您的首选宽度]; }。修改 [您的首选宽度] 以设置页面宽度。保存更改并清除缓存(可选)。

在 WordPress 中创建产品页面:1. 创建产品(名称、描述、图片);2. 自定义页面模板(添加标题、描述、图片、按钮);3. 输入产品信息(库存、尺寸、重量);4. 创建变体(不同颜色、尺寸);5. 设置可见性(公开或隐藏);6. 启用/禁用评论;7. 预览并发布页面。

WordPress 文章存储在 /wp-content/uploads 文件夹中。该文件夹使用子文件夹对不同类型的上传进行分类,包括按年、月和文章 ID 组织的文章。文章文件以纯文本格式 (.txt) 存储,文件名通常包含其 ID 和标题。

WordPress 模板文件位于 /wp-content/themes/[主题名称]/ 目录。它们用于决定网站的外观和功能,包括页眉(header.php)、页脚(footer.php)、主模板(index.php)、单个文章(single.php)、页面(page.php)、存档(archive.php)、类别(category.php)、标签(tag.php)、搜索(search.php)和 404 错误页面(404.php)。通过编辑和修改这些文件,可以自定义 WordPress 网站的外

在 WordPress 中搜索作者:1. 登录管理面板后,导航到“文章”或“页面”,使用搜索栏输入作者姓名,在“过滤器”中选择“作者”。2. 其他技巧:使用通配符扩大搜索范围,使用运算符组合条件,或输入作者 ID 以搜索文章。

最稳定的 WordPress 版本是最新版本,因为它包含最新的安全补丁、增强性能并引入新特性和改进。为了更新到最新版本,请登录 WordPress 仪表盘,转到“更新”页面并单击“立即更新”。

WordPress 采用 PHP 语言开发,作为其核心编程语言,用于处理数据库交互、表单处理、动态内容生成和用户请求。PHP 被选择的原因包括跨平台兼容性、易于学习、活跃社区以及丰富的库和框架。除了 PHP,WordPress 还使用 HTML、CSS、JavaScript、SQL 等语言来增强其功能。
