自定义帖子状态未出现

问题描述 投票:0回答:5

我正在为我的客户构建一个目录主题,我喜欢通过将帖子状态从发布修改为过期来在帖子中添加过期功能。

为了实现这一目标,我尝试使用以下代码注册新的帖子状态:

add_action('init',    'registerStatus', 0);

function registerStatus()
{
    $args = array(
        'label'                     =>  _x('Expired', 'Status General Name', 'z' ),
        'label_count'               =>  _n_noop('Expired (%s)',  'Expired (%s)', 'z'),
        'public'                    =>  true,
        'show_in_admin_all_list'    =>  true,
        'show_in_admin_status_list' =>  true,
        'exclude_from_search'       =>  true
    );

    register_post_status('expired', $args);
}

问题是我无法在 WordPress 帖子中看到注册的帖子状态,也无法在我的自定义帖子类型帖子状态中看到。

我做错了什么吗?

wordpress wordpress-theming
5个回答
9
投票

感谢 Ryan Bayne,我能够在编辑帖子页面上的管理面板中添加自定义帖子状态。没有可用的 WordPress 过滤器。他使用 jQuery 的解决方案非常完美。如果其他人正在寻找解决方案,请使用以下代码:

add_action( 'post_submitbox_misc_actions', 'my_post_submitbox_misc_actions' );
    function my_post_submitbox_misc_actions(){

    global $post;

    //only when editing a post
    if( $post->post_type == 'post' ){

        // custom post status: approved
        $complete = '';
        $label = '';   

        if( $post->post_status == 'approved' ){
            $complete = 'selected=\"selected\"';
            $label = '<span id=\"post-status-display\"> Approved</span>';
        }

        echo '<script>'.
                 'jQuery(document).ready(function($){'.
                     '$("select#post_status").append('.
                         '"<option value=\"approved\" '.$complete.'>'.
                             'Approved'.
                         '</option>"'.
                     ');'.
                     '$(".misc-pub-section label").append("'.$label.'");'.
                 '});'.
             '</script>';
    }
}

6
投票

自定义帖子状态功能仍在开发中(过去四年一直如此!),请参阅 https://core.trac.wordpress.org/ticket/12706,以及 https://wordpress 上的评论.stackexchange.com/q/67655/25765。更多有用信息请参见:https://wordpress.stackexchange.com/search?q=register_post_status

就我个人而言,我强烈反对实现自定义帖子状态,但如果确实有必要,您可以看看 Edit Flow 插件如何处理它。


2
投票

此功能仍有待未来开发

注意: 此功能不会将注册帖子状态添加到管理面板。该功能有待未来开发。请参阅Trac Ticket #12706。考虑使用操作钩子 post_submitbox_misc_actions 添加此参数。


1
投票

现在已经是 2014 年 11 月,自定义状态仍然存在问题。我认为发布的原始代码很好。这是一个视频,显示了您在实现自定义帖子状态时会遇到的问题。可能有一个解决方法,即连接到帖子查询并执行自定义查询,但我还没有开始研究。

应用自定义状态时,帖子的截屏不会显示在“全部”表中,但可以在每个自定义状态的表视图中找到帖子。点击此处查看短片。

该截屏视频是在我开发新的 WTG 任务管理器插件时拍摄的。我会将我的设计保留在插件中,希望它有助于鼓励 WordPress 这方面的改进。

为了正确的答案...我的自定义状态确实显示在我的自定义帖子类型的编辑帖子屏幕上,以便这是可能的。如果您想查看我的自定义帖子类型和状态的插件注册,请转到目录“posttypes/tasks.php”并使用一个工作示例。这是插件官方页面...

https://wordpress.org/plugins/wtg-tasks-manager/


1
投票

由于已知错误,帖子状态未显示 - https://core.trac.wordpress.org/ticket/12706。可以使用 GitHub 上的插件 WP Statuses 绕过它。

  1. 安装 WP Statuses 插件
  2. 相应修改代码:
add_action('init', 'new_post_status_add');

function new_post_status_add () {
    register_post_status('refused', array(
        'label'                     => _x('Refused', 'post'),
        'public'                    => true,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop('Refused <span class="count">(%s)</span>', 'Refused <span class="count">(%s)</span>'),

        /* WP Statuses specific arguments. */
        'post_type'                   => array( 'post' ), // Only for posts!
        'show_in_metabox_dropdown'    => true,
        'show_in_inline_dropdown'     => true,
        'show_in_press_this_dropdown' => true,
        'labels'                      => array(
            'metabox_dropdown' => __( 'Refused', 'wp-statuses' ),
            'inline_dropdown'  => __( 'Refused', 'wp-statuses' ),
        ),
        'dashicon'                    => 'dashicons-archive', //if you like
    ));
}
© www.soinside.com 2019 - 2024. All rights reserved.