首页 > CMS教程 > &#&按 > WordPress指南的自定义写作面板

WordPress指南的自定义写作面板

Jennifer Aniston
发布: 2025-02-21 08:55:09
原创
1000 人浏览过

WordPress 3.0进行了重大更新,包括WordPress MU的集成(启用多站点管理)和引入自定义邮政类型。 自定义帖子类型增强的一个特别有用的功能是自定义写作面板。

Guide to Wordpress's Custom Write Panels 自定义写入面板在邮政编辑器中提供可自定义的表单字段(文本输入,复选框等),并链接到自定义字段。虽然可以使用默认的自定义字段面板,但对于广泛的数据输入可能会很麻烦。自定义写作面板提供了简化的,视觉上吸引人的替代方案。

>让我们用“书籍”自定义帖子类型说明。 除了标准标题和内容之外,我们还将添加“作者”和“ ISBN”字段。 在您的主题的

>中,添加此代码以注册自定义帖子类型:>

functions.php>注册“书籍”帖子类型。 接下来,将以下内容添加到

>以创建自定义写面板:
add_action( 'init', 'create_book_type' );
function create_book_type() {
  register_post_type( 'books', array(
    'labels' => array(
      'name' => __( 'Books' ),
      'singular_name' => __( 'Book' )
    ),
    'public' => true,
  ) );
}
登录后复制

>在主题中创建Afunctions.php>目录,然后添加

(用于造型)和
// Define paths (adjust as needed)
define( 'MY_WORDPRESS_FOLDER', $_SERVER['DOCUMENT_ROOT'] );
define( 'MY_THEME_FOLDER', str_replace("\",'/',dirname(__FILE__)) );
define( 'MY_THEME_PATH', '/' . substr( MY_THEME_FOLDER, stripos(MY_THEME_FOLDER,'wp-content') ) );

add_action('admin_init','book_meta_init');
function book_meta_init() {
  wp_enqueue_style( 'my_meta_css', MY_THEME_PATH . '/custom/book_panel.css' );
  add_meta_box( 'book_meta', 'Book Information', 'book_meta', 'books', 'advanced', 'high' );
}

function book_meta() {
  global $post;
  $author = get_post_meta($post->ID,'author',TRUE);
  $isbn = get_post_meta($post->ID,'isbn',TRUE);
  include(MY_THEME_FOLDER . '/custom/book_information.php');
  wp_nonce_field( __FILE__, 'my_meta_noncename' );
}

function my_meta_save($post_id) {
  if (!wp_verify_nonce( $_POST['my_meta_noncename'], __FILE__ )) return $post_id;
  if (!current_user_can('edit_post', $post_id)) return $post_id;
  $accepted_fields['books'] = array( 'author', 'isbn' );
  $post_type_id = $_POST['post_type'];
  foreach ($accepted_fields[$post_type_id] as $key) {
    $custom_field = $_POST[$key];
    if (is_null($custom_field)) delete_post_meta($post_id, $key);
    elseif (isset($custom_field) && !is_null($custom_field)) update_post_meta($post_id,$key,$custom_field);
  }
  return $post_id;
}
add_action('save_post','my_meta_save');
登录后复制
>(对于面板HTML)。custombook_panel.css book_information.php

book_panel.css

.book_panel .description { display: none; }
.book_panel label { display: block; font-weight: bold; margin: 6px; margin-bottom: 0; margin-top: 12px; }
.book_panel label span { display: inline; font-weight: normal; }
.book_panel span { color: #999; display: block; }
.book_panel textarea, .book_panel input[type='text'] { margin-bottom: 3px; width: 100%; }
.book_panel h4 { color: #999; font-size: 1em; margin: 15px 6px; text-transform:uppercase; }
登录后复制
>最后,要在主题中显示自定义字段,请在循环中使用

book_information.php

<div class="book_panel">
  <h4>Book Details</h4>
  <label for="author">Author <span>(Required)</span></label>
  <input type="text" name="author" id="author" value="<?php echo esc_attr( $author ); ?>" /><br/>
  <label for="isbn">ISBN <span>(Required)</span></label>
  <input type="text" name="isbn" id="isbn" value="<?php echo esc_attr( $isbn ); ?>" />
</div>
登录后复制
这将完成设置。 切记调整代码中的路径以匹配主题的结构。 有关更多深入的WordPress知识,请考虑我们的出版物“构建您自己的邪恶的WordPress主题”。

以上是WordPress指南的自定义写作面板的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板