仅适用于特定自定义类型的自定义元字段

时间:2013-08-22 作者:crs1138

我有一个自定义的帖子类型curiculum 以及将值保存到自定义字段的元框。但是,此自定义字段被添加到另一个自定义帖子类型(由电子商务插件创建)wpsc-product.

这是处理元盒的代码片段。问题是,如何防止将自定义字段添加到wpsc-product?

/* Curiculum Date Metabox */
function curiculum_date_fields() {
if ( get_post_type() == \'curiculum\'):
    global $post;
    $custom = get_post_custom( $post->ID );
    $curiculum_date_from = $custom["curiculum_date_from"][0];
    ?>
    <p>
        <label for="curiculum_date_from">Date:</label><br />
        <input size="32" type="text" name="curiculum_date_from" id="curiculum_date_from" value="<?php echo $curiculum_date_from; ?>" />
<?php
endif;
}

function add_curiculum_date_box() {
    add_meta_box("curiculum_date_info", "Event Date", "curiculum_date_fields", "curiculum", "side");
}

function save_curiculum_date() {
    global $post;
    update_post_meta($post->ID, "curiculum_date_from", $_POST["curiculum_date_from"]);
}
add_action(\'admin_init\', \'add_curiculum_date_box\');
add_action(\'save_post\', \'save_curiculum_date\');
add_action(\'publish_post\', \'save_curiculum_date\');
编辑:代码curiculum 自定义帖子类型:

add_action(\'init\', \'curiculum_register\');

function curiculum_register() {

$labels = array(
    \'name\' => _x(\'CV Events\', \'post type general name\'),
    \'singular_name\' => _x(\'CV Event\', \'post type singular name\'),
    \'add_new\' => _x(\'Add New\', \'testimonials item\'),
    \'add_new_item\' => __(\'Add New Event\'),
    \'edit_item\' => __(\'Edit Event\'),
    \'new_item\' => __(\'New Event\'),
    \'view_item\' => __(\'View Event\'),
    \'search_items\' => __(\'Search Events\'),
    \'not_found\' =>  __(\'Nothing found\'),
    \'not_found_in_trash\' => __(\'Nothing found in Trash\'),
    \'parent_item_colon\' => \'\'
);

$args = array(
    \'labels\' => $labels,
    \'public\' => true,
    \'publicly_queryable\' => true,
    \'show_ui\' => true,
    \'query_var\' => true,
    \'rewrite\' => true,
    \'capability_type\' => \'post\',
    \'hierarchical\' => false,
    \'menu_position\' => 7,
    \'exclude_from_search\' => true,
    \'supports\' => array(\'title\',\'editor\', \'custom-fields\')
      );

    register_post_type( \'curiculum\' , $args );
 }

3 个回复
SO网友:JMau

EDIT: 我还建议您使用nonces 保存前检查字段。

EDIT2: Nonces(Nonces)

在字段前定义一个nonce:

wp_nonce_field( \'curiculum_meta_box_nonce\', \'meta_box_nonce\' );
您的保存功能不正确,请改用此功能:

  function save_curiculum_date($postid) {
    //stop if autosave
    if( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) return;

    // if our nonce isn\'t there, or we can\'t verify it, stop
    if( !isset( $_POST[\'meta_box_nonce\'] ) || !wp_verify_nonce( $_POST[\'meta_box_nonce\'], \'curiculum_meta_box_nonce\' ) ) return;

    // if our current user can\'t edit this post, stop
    if( !current_user_can( \'edit_post\' ) ) return;

update_post_meta($postid, "curiculum_date_from", $_POST["curiculum_date_from"]);
}
fields函数不正确,请改用此函数:

$curiculum_date_from = get_post_meta(get_the_ID(),\'curiculum_date_from\',true);
?>
<p>
    <label for="curiculum_date_from">Date:</label><br />
    <input size="32" type="text" name="curiculum_date_from" id="curiculum_date_from" value="<?php echo $curiculum_date_from; ?>" />

SO网友:iEmanuele

删除操作后:

add_action(\'admin_init\', \'add_curiculum_date_box\');

你可以试试这个:

function add_curiculum_date_box($post){
      add_meta_box(
        "curriculum_date_info", 
        "Event Date", 
        "curiculum_date_fields", 
        $post->post_type,
        "side"
      );
}
并将其添加到您的CPT$参数中:

\'register_meta_box_cb\' => \'add_curiculum_date_box\'

注册此post类型时,它还将设置为运动事件,这些事件将在相应的写入屏幕中创建元框。它省去了使用单独调用add\\u action来设置元框的麻烦。它还将单个post类型的所有代码保存在一个地方。在功能上,它的作用与“add\\u meta\\u box”变量挂钩完全相同。这只是完成相同任务的另一种方式。

Credits

SO网友:Gabriel Barina

以防万一,您没有找到一个简单而优雅的解决方案,或者如果其他人偶然发现了这个问题(或类似的问题),请使用WordPress Creation Kit 来自wordpress。org用户可以轻松创建CPT或添加元框(如果有任何疑问,请阅读插件的描述)。

结束

相关推荐