自动将帖子标题设置为与元框相同的值

时间:2011-03-14 作者:INT

我已经从我创建的自定义帖子类型中删除了“Title”。这使得所有新帖子都被命名为“自动草稿”。

为了解决这个问题,我希望能够从元框中提取一个值/帖子名称,并将其保存为新标题。这可能吗?

这就是我正在使用的,这个meta的值应该是新的标题

 function save_title_meta($post_id, $post) {

// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST[\'meta_noncename\'], plugin_basename(__FILE__) )) {
return $post->ID;
}

// Is the user allowed to edit the post or page?
if ( !current_user_can( \'edit_post\', $post->ID ))
    return $post->ID;

// OK, we\'re authenticated: we need to find and save the data
// We\'ll put it into an array to make it easier to loop though.

$project_meta[\'_title\'] = $_POST[\'_title\'];

// Add values of $project_meta as custom fields

foreach ($project_meta as $key => $value) { // Cycle through the $project_meta array!
    if( $post->post_type == \'revision\' ) return; // Don\'t store custom data twice
    $value = implode(\',\', (array)$value); // If $value is an array, make it a CSV (unlikely)
    if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
        update_post_meta($post->ID, $key, $value);
    } else { // If the custom field doesn\'t have a value
        add_post_meta($post->ID, $key, $value);

    }
    if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
}

 }

add_action(\'save_post\', \'save_title_meta\', 1, 2); // save the custom fields
?>




echo \'<input type="hidden" name="meta_noncename" id="meta_noncename" value="\' .
wp_create_nonce( plugin_basename(__FILE__) ) . \'" />\';

// Get the location data if its already been entered
$title = get_post_meta($post->ID, \'_title\', true);

// Echo out the field
echo \'<input type="text" name="_title" value="\' . $title  . \'" class="widefat" />\';
?>

1 个回复
最合适的回答,由SO网友:TheDeadMedic 整理而成

即使您已禁用title 对于自定义帖子类型,如果使用帖子名称post_title 对于您自己的标题输入,WordPress仍将使用它并相应地更新文章标题。

结束

相关推荐