Answers:
我通过挂钩post-new.php
并检查category_id
请求参数来解决此问题。如果它不存在,我将显示一个带有类别下拉列表的表单,该表单将提交回到此页面,然后进行调用,exit()
因此不会显示常规的张贴表单。如果存在,我为此设置一个钩子wp_insert_post
,以添加类别。这工作,因为一个新的职位是通过该数据库已经创建的get_default_post_to_edit()
功能,我们可以添加类别,标签,或其他(元)的内容。此后,将使用“新”新内容来呈现表单。
add_filter( 'load-post-new.php', 'wpse14403_load_post_new' );
function wpse14403_load_post_new()
{
$post_type = 'post';
if ( isset( $_REQUEST['post_type'] ) ) {
$post_type = $_REQUEST['post_type'];
}
// Only do this for posts
if ( 'post' != $post_type ) {
return;
}
if ( array_key_exists( 'category_id', $_REQUEST ) ) {
add_action( 'wp_insert_post', 'wpse14403_wp_insert_post' );
return;
}
// Show intermediate screen
extract( $GLOBALS );
$post_type_object = get_post_type_object( $post_type );
$title = $post_type_object->labels->add_new_item;
include( ABSPATH . 'wp-admin/admin-header.php' );
$dropdown = wp_dropdown_categories( array(
'name' => 'category_id[]',
'hide_empty' => false,
'echo' => false,
) );
$category_label = __( 'Category:' );
$continue_label = __( 'Continue' );
echo <<<HTML
<div class="wrap">
<h2>{$title}</h2>
<form method="get">
<table class="form-table">
<tbody>
<tr valign="top">
<th scope="row">{$category_label}</th>
<td>{$dropdown}</td>
</tr>
<tr>
<td></td>
<th><input name="continue" type="submit" class="button-primary" value="{$continue_label}" /></th>
</tbody>
</table>
<input type="hidden" name="post_type" value="{$post_type}" />
</form>
</div>
HTML;
include( ABSPATH . 'wp-admin/admin-footer.php' );
exit();
}
// This function will only be called when creating an empty post,
// via `get_default_post_to_edit()`, called in post-new.php
function wpse14403_wp_insert_post( $post_id )
{
wp_set_post_categories( $post_id, $_REQUEST['category_id'] );
}
post-new.php
在主题主题functions.php
或插件文件中添加此代码。