Rumah >Tutorial CMS >WordTekan >Panduan untuk Panel Tulis Khusus WordPress ' s
WordPress 3.0 mengantar kemas kini yang ketara, termasuk integrasi WordPress MU (membolehkan pengurusan berbilang tapak) dan pengenalan jenis pos tersuai. Ciri yang sangat berguna yang dipertingkatkan oleh jenis pos tersuai adalah panel tulis tersuai.
mari kita menggambarkan dengan jenis pos "buku". Di luar tajuk dan kandungan standard, kami akan menambah bidang "Pengarang" dan "ISBN". Dalam tema anda
, tambahkan kod ini untuk mendaftarkan jenis pos tersuai: 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>ini mendaftarkan jenis "buku". Seterusnya, tambahkan yang berikut ke
untuk membuat panel tulis tersuai: functions.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>
: custom
book_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>dalam gelung anda:
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>
Atas ialah kandungan terperinci Panduan untuk Panel Tulis Khusus WordPress ' s. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!