WordPress给文章添加自定义字段

示例

在文章中添加自定义字段 keywords 和 description

 在当前主题的function.php下加入以下代码

/* Define the custom box,适用WP 3.0以后的版本 */
add_action( 'add_meta_boxes', 'ludou_add_custom_box' );

// 如果是WP 3.0之前的版本,使用以下一行代码
// add_action( 'admin_init', 'ludou_add_custom_box', 1 );

/* Do something with the data entered */
add_action( 'save_post', 'ludou_save_postdata' );

/* Adds a box to the main column on the Post and Page edit screens */
function ludou_add_custom_box() {
  add_meta_box(
    'ludou_sectionid',
    'SEO', // 可自行修改标题文字
    'ludou_inner_custom_box',
    'post'
  );
}

/* Prints the box content */
function ludou_inner_custom_box( $post ) {
  global $wpdb;
   
  // Use nonce for verification
  wp_nonce_field( plugin_basename( __FILE__ ), 'ludou_noncename' );
   
  // 获取固定字段keywords和description的值,用于显示之前保存的值
  // 此处wp_posts新添加的字段为keywords和description,多个用半角逗号隔开
  $date = $wpdb->get_row( $wpdb->prepare( "SELECT keywords, description FROM $wpdb->posts WHERE ID = %d", $post->ID) );

  // Keywords 字段输入框的HTML代码
  echo '<label for="keywords_new_field">Keywords</label> ';
  echo '<input type="text" id="keywords_new_field" name="keywords_new_field" value="'.$date->keywords.'" size="18" />';

  // description 字段输入框的HTML代码,即复制以上两行代码,并将keywords该成description
  echo '<label for="description_new_field">Description</label> ';
  echo '<input type="text" id="description_new_field" name="description_new_field" value="'.$date->description.'" size="18" />';
  // 多个字段依此类推
}

/* 文章提交更新后,保存固定字段的值 */
function ludou_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['ludou_noncename'], plugin_basename( __FILE__ ) ) )
      return;
 
  // 权限验证
  if ( 'post' == $_POST['post_type'] ) {
    if ( !current_user_can( 'edit_post', $post_id ) )
        return;
  }

  // 获取编写文章时填写的固定字段的值,多个字段依此类推
  $keywords = $_POST['keywords_new_field'];
  $description = $_POST['description_new_field'];
   
  // 更新数据库,此处wp_posts新添加的字段为keywords和description,多个根据你的情况修改
  global $wpdb;
  $wpdb->update( "$wpdb->posts",
          // 以下一行代码,多个字段的话参照下面的写法,单引号中是字段名,右边是变量值。半角逗号隔开
          array( 'keywords' => $keywords, 'description' => $description ),
          array( 'ID' => $post_id ),
          // 添加了多少个新字段就写多少个%s,半角逗号隔开
          array( '%s', '%s' ),
          array( '%d' )  
  );
}

在 wp_post 表里添里对应列 keywords 和 description

添加字段

 

 使用 get_row() 获取自定义的内容

global $wpdb;
// $post->ID是文章id,自行修改
$date = $wpdb->get_row( $wpdb->prepare( "SELECT keywords, description FROM $wpdb->posts WHERE ID = %d", $post->ID) );

// keywords值
$keywords = $date->keywords;

// description值
$description = $date->description;

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值