自定义帖子类型中的Wordpress自定义字段

时间:2011-02-03 13:01:10

标签: php wordpress

我购买的主题在帖子/产品编辑器中没有自定义字段。它在自己的admin php文件中使用自定义帖子类型。我已经设法通过从wordpress核心metabox.php文件中提取以下代码来添加自定义字段但是我不确定如何让它工作。它缺少自定义字段值应该去的区域。

<div id="postcustomstuff">
<div id="ajax-response"></div>
<?php
$metadata = has_meta($post->ID);
list_meta($metadata);
meta_form(); ?>
</div>

2 个答案:

答案 0 :(得分:8)

开发人员在调用register_post_type时忘了支持“自定义字段”。

编辑帖子时,如果自定义字段的屏幕选项下没有复选框,那就是原因。在我的插件的init钩子中,我做...

register_post_type('mynamespace_product',
    array('labels' => array(
            'name' => __( 'Products' ),
            'singular_name' => __( 'Product' )
            ),
        'taxonomies' => array('category', 'product_type'), // this is IMPORTANT,                
        'public' => true,
        'has_archive' => true,
        'supports' => array('title','editor','custom-fields','comments')    
        )           
    );

答案 1 :(得分:2)

要获取与帖子相关联的自定义字段,您可以这样查询:

 if ( get_post_meta($post->ID, 'my_customfield', true) ) : 
 echo get_post_meta($post->ID, 'my_customfield', true) 
 endif; 

希望这有帮助