duanguai2781 2012-10-09 21:47
浏览 72
已采纳

管理面板中Wordpress Post Array的奇怪问题

I am incredibly confused over an issue with the Post Array on a Wordpress Site I am building for a client. I'm hoping someone has seen this before!

I have registered a Custom Post Type:

register_post_type( 'products', array(
    'labels' => array(
        'name' => __( 'Products' ),
        'singular_name' => __( 'Product' ),
    ),
    'public' => true,
    'menu_position' => 20,
    'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'comments', ),
    'taxonomies' => array('category'),
    'capability_type' => 'post',
    'rewrite' => array( 'slug' => 'products','with_front' => TRUE),
    'register_meta_box_cb' => 'product_page_meta_box'
));

Within which I have registered a callback for the following meta box:

function product_page_meta_box() {
    add_meta_box('product_meta_box_content', 'Product Specifications',  'product_meta_box_content', 'products');
}

function product_meta_box_content( $post ) {

    wp_nonce_field( plugin_basename( __FILE__ ), 'product_noonce' );

    echo '<div class="inside>';
        var_dump($post);
        echo '<p>Please detail the product specifics here:</p>';
    echo '</div>';
}

The frustration I have is that the result of the var_dump above on the admin page is:

string(3) "258" ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2012-08-27 19:33:30" ["post_date_gmt"]=> string(19) "2012-08-27 19:33:30" ["post_content"]=> string(0) "" ["post_title"]=> string(10) "The Wensum" ["post_excerpt"]=> string(20) "Testing the excerpt!" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(22) "large-6-perch-dovecote" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2012-10-04 16:50:19" ["post_modified_gmt"]=> string(19) "2012-10-04 16:50:19" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> string(1) "0" ["guid"]=> string(53) "http://dovecotes.local//?post_type=products&p=258" ["menu_order"]=> string(1) "0" ["post_type"]=> string(8) "products" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["ancestors"]=> array(0) { } ["filter"]=> string(4) "edit" }

In the admin area it has no type and is completely invalid; on the the page concerned though the post array is fine, the ID has a key and it is a valid object:

object(stdClass)#145 (25) { ["ID"]=> int(258) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2012-08-27 19:33:30" ["post_date_gmt"]=> string(19) "2012-08-27 19:33:30" ["post_content"]=> string(0) "" ["post_title"]=> string(10) "The Wensum" ["post_excerpt"]=> string(20) "Testing the excerpt!" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(22) "large-6-perch-dovecote" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2012-10-04 16:50:19" ["post_modified_gmt"]=> string(19) "2012-10-04 16:50:19" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(54) "http://dovecotes.local//?post_type=products&p=258" ["menu_order"]=> int(0) ["post_type"]=> string(8) "products" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["ancestors"]=> array(0) { } ["filter"]=> string(3) "raw" }
  • 写回答

1条回答 默认 最新

  • dongzhazhuo0572 2012-10-10 02:39
    关注

    I can't guarantee this will work, but I've always avoided using the 'register_meta_box_cb' when defining custom post types in favor of the 'add_meta_boxes' action hook.

    Apart from the extra comma in your 'supports' parameter, your code looks right, so nothing in your example would lead me to believe that any of it could be the culprit unless it's a prioritization discrepancy. I'm also not sure if the id and callback parameters are allowed to be identical in your metabox arguments, so I took the liberty of changing that too:

    register_post_type( 'products', array(
        'labels' => array(
            'name' => __( 'Products' ),
            'singular_name' => __( 'Product' ),
        ),
        'public' => true,
        'menu_position' => 20,
        'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'comments' ),
        'taxonomies' => array('category'),
        'capability_type' => 'post',
        'rewrite' => array( 'slug' => 'products','with_front' => TRUE)
    ));
    add_action('add_meta_boxes', 'product_page_meta_box');
    function product_page_meta_box() {
        add_meta_box('product_meta_box_content', 'Product Specifications',  'product_meta_box_content_cb', 'products');
    }
    
    function product_meta_box_content_cb( $post ) {
    
        wp_nonce_field( plugin_basename( __FILE__ ), 'product_noonce' );
    
        echo '<div class="inside>';
            var_dump($post);
            echo '<p>Please detail the product specifics here:</p>';
        echo '</div>';
    }
    

    If that doesn't help, then I'm not sure what else it would be without seeing more code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)