【问题标题】:Strange Issue with Wordpress Post Array in Admin Panel管理面板中 Wordpress 帖子数组的奇怪问题
【发布时间】:2012-09-30 07:24:19
【问题描述】:

对于我为客户构建的 Wordpress 网站上的 Post Array 问题,我感到非常困惑。我希望有人以前见过这个!

我已经注册了一个自定义帖子类型:

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'
));

我在其中注册了以下元框的回调:

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>';
}

让我感到沮丧的是,上面管理页面上 var_dump 的结果是:

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" }

在管理区它没有类型,完全无效;虽然帖子数组很好,但在相关页面上,ID 有一个键,它是一个有效的对象:

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" }

【问题讨论】:

    标签: php wordpress custom-post-type meta-boxes


    【解决方案1】:

    我不能保证这会奏效,但在定义自定义帖子类型时,我一直避免使用“register_meta_box_cb”,而是使用“add_meta_boxes”操作挂钩。

    除了“支持”参数中的额外逗号之外,您的代码看起来正确,因此您的示例中没有任何内容会让我相信任何一个都可能是罪魁祸首,除非它是优先级差异。我也不确定你的元框参数中是否允许 id 和回调参数相同,所以我也冒昧地改变了它:

    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>';
    }
    

    如果这没有帮助,那么如果没有看到更多代码,我不确定还会发生什么。

    【讨论】:

    • 您好,感谢@maiorano84 的回复。两种方法我都试过了,不幸的是结果是一样的,你认为什么代码会有帮助?
    • 抱歉,错过了关于回调的部分,ID 将在稍后更改这些内容,如果有帮助,请告诉您!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-08
    • 2015-05-07
    • 2014-02-23
    • 2019-02-12
    • 2013-12-16
    • 1970-01-01
    相关资源
    最近更新 更多