如何在WordPress中添加自定义文章状态功能?
请提供在WordPress中创建和管理自定义文章状态功能的方法。
-
玖比肆 评论以下是在WordPress中创建和管理自定义文章状态功能的步骤:
1. 创建自定义文章状态
在functions.php文件中添加以下代码:
```
function my_custom_post_status() {
register_post_status( 'my-custom-status', array(
'label' => __( 'My Custom Status', 'text_domain' ),
'public' => true,
'exclude_from_search' => true,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'My Custom Status (%s)', 'My Custom Status (%s)', 'text_domain' )
) );
}
add_action( 'init', 'my_custom_post_status' );
```此代码将创建一个名为“my-custom-status”的自定义文章状态,并将其显示在管理员目录中的所有文章列表和文章状态下拉菜单中。
2. 在文章编辑器页面中添加自定义状态
在WordPress文章编辑器界面中,单击右上角的“状态和可见性”选项卡。可以将现有文章状态从“草稿”更改为新创建的自定义状态。
3. 添加自定义状态筛选器
要在WordPress管理员列表页面中添加一个自定义文章状态筛选器,请在functions.php文件中添加以下代码:
```
function my_custom_status_filter( $query ) {
global $post_type, $pagenow;
if ( $post_type == 'post' && $pagenow == 'edit.php' && isset( $_GET['my-custom-status'] ) && $_GET['my-custom-status'] != '' ) {
$query->query_vars['post_status'] = $_GET['my-custom-status'];
}
}
add_filter( 'parse_query', 'my_custom_status_filter' );function my_custom_status_dropdown() {
$statuses = array(
'my-custom-status' => 'My Custom Status'
);
$output = '';
if ( isset( $_GET['my-custom-status'] ) && $_GET['my-custom-status'] != '' ) {
$current = $_GET['my-custom-status'];
} else {
$current = 'all';
}
foreach ( $statuses as $key => $value ) {
if ( $current == $key ) {
$output .= '';
} else {
$output .= '';
}
}
echo '';
}
add_action( 'restrict_manage_posts', 'my_custom_status_dropdown' );
```此代码将添加一个名为“My Custom Status”的筛选器下拉菜单,用户可以使用该筛选器查看对应状态的文章。
以上就是在WordPress中添加自定义文章状态功能的方法。
2023年 5月 13日 上午4:59
