自定义帖子类型未显示自定义字段

时间:2016-07-30 作者:user99238

我对WordPress开发有点陌生,所以很沮丧——我创建了一些自定义的帖子类型,并试图向其中添加自定义字段。由于某些原因,当我尝试添加新帖子时,字段将不会显示!!!这是我的代码——它位于自定义post类型文件中。

<?php
/**
 * Plugin Name: Art Beat
 * Plugin URI: #
 * Version: 1.0
 * Author: bjorkland
 * Author URI: http://localhost:8888/wp-admin/plugins.php?plugin_status=all&paged=1&s
 * Description: A custom post type
 * License: GPL2
 */

class ArtBeat {

function __construct() {
  add_action( \'init\', array( $this, \'register_custom_post_type\' ) );
}
function register_custom_post_type() {
register_post_type( \'artbeat\', array(
    \'labels\' => array(
        \'name\'               => _x( \'Art Beats\', \'post type general name\', \'artbeat\' ),
        \'singular_name\'      => _x( \'Art Beat\', \'post type singular name\', \'artbeat\' ),
        \'menu_name\'          => _x( \'Art Beat\', \'admin menu\', \'artbeat\' ),
        \'name_admin_bar\'     => _x( \'Art Beat\', \'add new on admin bar\', \'artbeat\' ),
        \'add_new\'            => _x( \'Add New Art Beat\', \'art\', \'artbeat\' ),
        \'add_new_item\'       => __( \'Add New Art Beat\', \'artbeat\' ),
        \'new_item\'           => __( \'New Art Beat\', \'artbeat\' ),
        \'edit_item\'          => __( \'Edit Art Beat\', \'artbeat\' ),
        \'view_item\'          => __( \'View Art Beat\', \'artbeat\' ),
        \'all_items\'          => __( \'All Art Beats\', \'artbeat\' ),
        \'search_items\'       => __( \'Search Art Beats\', \'artbeat\' ),
        \'parent_item_colon\'  => __( \'Parent Art Beat:\', \'artbeat\' ),
        \'not_found\'          => __( \'No Art Beat found.\', \'artbeat\' ),
        \'not_found_in_trash\' => __( \'No Art Beat found in Trash.\', \'artbeat\' ),
    ),

    // Frontend
    \'has_archive\'        => false,
    \'public\'             => false,
    \'publicly_queryable\' => false,

    // Admin
    \'capability_type\' => \'post\',
    \'menu_icon\'     => \'dashicons-businessman\',
    \'menu_position\' => 10,
    \'query_var\'     => true,
    \'show_in_menu\'  => true,
    \'show_ui\'       => true,
    \'supports\'      => array(
        \'title\',
        \'author\',
        \'comments\',
    ),
) );
}
}

$ArtBeat = new ArtBeat;


function add_artbeat_meta_boxes() {
 add_meta_box("artbeat_contact_meta", "Contact Details", "add_contact_details_artbeat_meta_box", "artbeats", "normal", "low");
}
   function add_contact_details_artbeat_meta_box()
  {
global $post;
$custom = get_post_custom( $post->ID );

?>
 <style>.width99 {width:99%;}</style>
 <p>
<label>Date:</label><br />
  <textarea rows="5" name="date" class="width99"><?= @$custom["date"][0] ?></textarea>
   </p>
   <p>
     <label>Museum:</label><br />
     <input type="text" name="museum" value="<?= @$custom["museum"][0] ?>"    class="width99" />
  </p>
  <?php
}

function save_artbeat_custom_fields(){
 global $post;

if ( $post )
{
  update_post_meta($post->ID, "date", @$_POST["date"]);
  update_post_meta($post->ID, "museum", @$_POST["museum"]);
}
}
add_action( \'admin_init\', \'add_artbeat_meta_boxes\' );
add_action( \'save_post\', \'save_artbeat_custom_fields\' );

1 个回复
SO网友:Aftab

如果您已经在插件中创建了一个类,那么您应该只在类中使用admin\\u init的操作挂钩,就像使用“init”一样

function __construct() {
  add_action( \'init\', array( $this, \'register_custom_post_type\' ) );
  add_action( \'admin_init\', array( $this, \'add_artbeat_meta_boxes\' ) );
  add_action( \'save_post\', array( $this, \'save_artbeat_custom_fields\' ) );
}
希望这能帮到你。

相关推荐

如何让`wp-list-table`显示我在Custom-Post中的`Custom-Fields`

一切都好吗<我需要wp-list-table 也要显示custom-fields 在每个custom-post 我有,但我不知道如何做到这一点,在这幅图中,它显示了带有字段的表格:Title, Author and Publication Date: 我想要的是能够选择custom-fields 将出现,例如以下示例Title, Carta, Naipe, Author, and Date of Publication: