首頁 >CMS教程 >&#&按 >WordPress指南的自定義寫作面板

WordPress指南的自定義寫作面板

Jennifer Aniston
Jennifer Aniston原創
2025-02-21 08:55:09996瀏覽

WordPress 3.0進行了重大更新,包括WordPress MU的集成(啟用多站點管理)和引入自定義郵政類型。 自定義帖子類型增強的一個特別有用的功能是自定義寫作面板。

Guide to Wordpress's Custom Write Panels 自定義寫入面板在郵政編輯器中提供可自定義的表單字段(文本輸入,複選框等),並鏈接到自定義字段。雖然可以使用默認的自定義字段面板,但對於廣泛的數據輸入可能會很麻煩。自定義寫作面板提供了簡化的,視覺上吸引人的替代方案。

>讓我們用“書籍”自定義帖子類型說明。 除了標準標題和內容之外,我們還將添加“作者”和“ ISBN”字段。 在您的主題的

>中,添加此代碼以註冊自定義帖子類型:>

functions.php>註冊“書籍”帖子類型。 接下來,將以下內容添加到

>以創建自定義寫面板:
<code class="language-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,
  ) );
}</code>

>在主題中創建Afunctions.php>目錄,然後添加

(用於造型)和
<code class="language-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');</code>
>(對於面板HTML)。 custombook_panel.css book_information.php

book_panel.css

<code class="language-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; }</code>
>最後,要在主題中顯示自定義字段,請在循環中使用

book_information.php

<code class="language-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></code>
這將完成設置。 切記調整代碼中的路徑以匹配主題的結構。 有關更多深入的WordPress知識,請考慮我們的出版物“構建您自己的邪惡的WordPress主題”。

以上是WordPress指南的自定義寫作面板的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn