更新/问题澄清:如何从此线程修改以下代码:Force category choice before creating new post? 哪个网站提供了一个下拉菜单来选择一个类别,然后再创建一篇新的帖子,将一些默认的html内容添加到一个特定的类别???
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\'] );
}
原始问题:我在一个电子商务网站工作;我想自动添加一些特定类别(如产品)中帖子的默认内容。
我找到了一种方法,可以在所有帖子中添加一些默认内容,但无法将其限制为一个类别。
在此期间,我最终使用了自定义的帖子类型,但要说服像最近的帖子和帖子滑块这样的小部件使用它们是一件很麻烦的事。
欢迎提出任何想法-谢谢