【问题标题】:Wordpress - custom post status is not showingWordpress - 自定义帖子状态未显示
【发布时间】:2017-04-27 13:29:58
【问题描述】:

我创建了新的帖子状态。这是我的自定义插件中的代码:

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>'),
    ));
}

但它不起作用 - 在编辑表单和快速编辑表单中不可见:

我应该怎么做才能使状态可用?

【问题讨论】:

    标签: wordpress post status


    【解决方案1】:

    抱歉延迟回复。我希望我的评论对其他人有所帮助。

    自定义帖子状态是一个未解决的问题,在 Wordpress 网站 here 上已经存在了很长时间,或者至少从 8 年前开始。

    Wordpress 在 codex 文档中注明了函数 register_post_status()

    此功能将已注册的帖子状态添加到管理面板。此功能有待未来开发。请参考 到Trac Ticket #12706。考虑动作钩子 post_submitbox_misc_actions 添加此参数。

    有一种变通方法可以用于我的一个项目。我混合了this urlthis url 上描述的解决方案。

    总结是 Wordpress 不会自动显示自定义帖子状态。它需要使用您的主题的function.php 进行操作。

    第一步

    创建自定义帖子状态,与您提到的相同。只是,不要忘记“不应在 'init' 操作之前调用此函数。”

    ‌
    function j_custom_post_status() {
    $args = array(
        'label' => _x( 'Refused', 'post'),
        'label_count' => _n_noop( 'Refused <span class="count">(%s)</span>',  'Refused (%s)', 'post' ),
        'public' => false,
        'internal' => true,
        'show_in_admin_all_list' => true,
        'show_in_admin_status_list' => true,
        'exclude_from_search' => false,
    );
    register_post_status( 'j_refused', $args );
    
    add_action( 'init', 'j_custom_post_status', 0 );
    ‌
    

    我使用WP Generator获取上面的代码sn-p。

    第二步

    调整管理面板以在帖子列表页面 edit.php 的快速编辑菜单 (QE) 中显示自定义状态。我们需要 JQuery 的帮助,我们需要将 JQuery 代码添加到管理面板页脚。

    ‌
    function j_append_status_list(){
    
        //array of all custom status that you created
        $arr_customstatus = array(
            'refused',
            'other_custom_status',
            );
    
         echo '<script>';
    
         //flush
         $tmp = '';
    
         //generate string
         foreach ($arr_customstatus as $pkey ) {
             $tmp.= '<option value="'. $pkey .'">'. $pkey .'</option>';
         }
    
         echo "jQuery(document).ready( function() { jQuery( 'select[name=\"_status\"]' ).append( ". $tmp ." );
    });";
    
         echo '</script>';
    
    }
    add_action('admin_footer-edit.php','j_append_status_list');
    ‌
    

    注意:以上功能不会自动选择QE中的状态,以防帖子状态被修改过。要将selected="true"添加到选项中,您需要使用global $post并检查当前状态,与下面第三步中使用的方法相同。

    第三步

    现在,我们还需要将自定义帖子状态元数据添加到帖子编辑页面。

    function j_custom_status_metabox(){
    
        global $post;
    
        $custom  = get_post_custom( $post->ID );
        $status  = $custom["_status"][0];
    
        // Array of custom status messages
        $arr_customstatus = array(
            'refused',
            'other_custom_status',
            );
    
            //Flush
            $tmp = '';
    
            foreach( $jstats as $pkey ) {
    
                if( $status == $pkey ){
                     $tmp.= '<option value="'.$pkey.'" selected="true">'. $pkey .'</option>';
                 } else {
                     $tmp.= '<option value="'. $pkey .'">'. $pkey .'</option>';
                 }
             }
    
         echo '<script>';
    
         echo "jQuery(document).ready( function() { jQuery( 'select[name=\"_status\"]' ).append( ". $tmp ." );
    });";
    
         echo '</script>';
    
    }
    add_action('admin_footer-post.php','j_custom_status_metabox');
    add_action('admin_footer-post-new.php','j_custom_status_metabox');
     ‌
    

    第四步

    如果您想根据自定义帖子状态在帖子列表中显示消息,您也可以添加此功能。但是,这是可选的。

    function j_display_status_label( $statuses ) {
    
        global $post; 
    
        //Display label on all posts list but not on the custom status list
        if( get_query_var( 'post_status' ) != 'refused' ){ 
            if( $post->post_status == 'refused' ){ 
                return array('Refused'); 
            }
        }
        return $statuses; 
    }
    
    add_filter( 'display_post_states', 'j_display_status_label' );
    ‌‌    ‌
    

    注意:上面的函数不包括所有未来的自定义状态。您需要添加foreach 循环以备不时之需。

    【讨论】:

      【解决方案2】:

      尝试使用此代码,

      // Register Custom Status
      function custom_post_status() {
      
          $args = array(
              'label'                     => _x( 'Refused', 'Status General Name', 'text_domain' ),
              'label_count'               => _n_noop( 'Refused (%s)',  ' (%s)', 'text_domain' ), 
              'public'                    => true,
              'show_in_admin_all_list'    => true,
              'show_in_admin_status_list' => true,
              'exclude_from_search'       => false,
          );
          register_post_status( 'refused', $args );
      
      }
      add_action( 'init', 'custom_post_status', 0 );
      

      我希望这对你有用。

      【讨论】:

        【解决方案3】:

        尝试将PHP_INT_MAX 添加到您的j_custom_post_status 操作中

        add_action( 'init', 'j_custom_post_status', PHP_INT_MAX  );
        

        【讨论】:

          猜你喜欢
          • 2015-01-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-12-29
          相关资源
          最近更新 更多