2010-10-21 36 views
11

在WordPress中,您將獲得默認發佈狀態:發佈,草稿和待審覈。是否可以通過註冊活動主題的function.php文件來添加更多的帖子類型?WordPress的創建一個新的職位狀態?

也可以編輯Publish Meta Box的標籤嗎?我提交的內容真的不是出版...

還想補充我只想在我的自定義文章類型中進行這些更改。

親切的問候

斯科特

+0

這些狀態對郵件的處理和顯示方式有着深遠的影響,不是嗎?我不認爲你可以添加一個新的列表。你想添加什麼新的狀態? – 2010-10-21 16:59:25

+0

我只想將新的狀態添加到我的自定義帖子類型中,並且這些帖子已經通過自定義查詢管理顯示帖子。使用wordpress,您可以查詢以顯示帖子,具體取決於它的狀態。因此,添加銷售和移除等一些狀態不應該是系統的一個問題? – Brady 2010-10-21 17:06:06

+1

@布萊迪我看到了。我不知道這是否可能。那麼使用標記/分類系統呢? – 2010-10-21 17:07:40

回答

18

由於WP 3.0,您可以使用register_post_status()函數(http://hitchhackerguide.com/2011/02/12/register_post_status/)向帖子類型添加新的狀態。

WP本身使用register_post_status()註冊默認的 「公佈」, 「草案」 等上使用create_initial_post_types()函數在初始化狀態WP-包括/ post.php中(http://hitchhackerguide.com/2011/02/11/create_initial_post_types/)。

看看這些鏈接中的代碼,您可以瞭解如何使用該功能。

我希望能幫助你開始!

2

,如果你知道如何你可以寫一個插件。你必須挖掘到的文檔或類似的插件,像這樣一個http://wordpress.org/extend/plugins/edit-flow/或這一個http://wordpress.org/extend/plugins/custom-post-type-ui/

隨着「掛鉤,操作和過濾器」,您可以更改管理界面,在這裏看到http://codex.wordpress.org/Plugin_API

到目前爲止,我已經寫只是一個簡單的插件,我不知道你必須遵循的具體步驟來完成這...

祝你好運!

+0

感謝您的建議凱恩,但這一刻我沒有時間去挖掘API,我目前正在爲客戶工作到最後期限,所以現在必須堅持自定義現場解決方案。我只是希望有人能知道如何去做,並提供一些關於如何使用的演示代碼。 – Brady 2010-10-22 08:49:47

1
/** 
* PostStatusExtender 
* 
* @author Hyyan Abo Fakher<[email protected]> 
*/ 
class PostStatusExtender 
{ 

    /** 
    * Extend 
    * 
    * Extend the current status list for the given post type 
    * 
    * @global \WP_POST $post 
    * 
    * @param string $postType the post type name , ex: product 
    * @param array $states array of states where key is the id(state id) and value 
    *      is the state array 
    */ 
    public static function extend($postType, $states) 
    { 

     foreach ($states as $id => $state) { 
      register_post_status($id, $state); 
     } 

     add_action('admin_footer-post.php', function() use($postType, $states) { 

      global $post; 
      if (!$post || $post->post_type !== $postType) { 
       return false; 
      } 

      foreach ($states as $id => $state) { 

       printf(
         '<script>' 
         . 'jQuery(document).ready(function($){' 
         . ' $("select#post_status").append("<option value=\"%s\" %s>%s</option>");' 
         . ' $("a.save-post-status").on("click",function(e){' 
         . '  e.preventDefault();' 
         . '  var value = $("select#post_status").val();' 
         . '  $("select#post_status").value = value;' 
         . '  $("select#post_status option").removeAttr("selected", true);' 
         . '  $("select#post_status option[value=\'"+value+"\']").attr("selected", true)' 
         . ' });' 
         . '});' 
         . '</script>' 
         , $id 
         , $post->post_status !== $id ? '' : 'selected=\"selected\"' 
         , $state['label'] 
       ); 

       if ($post->post_status === $id) { 
        printf(
          '<script>' 
          . 'jQuery(document).ready(function($){' 
          . ' $(".misc-pub-section #post-status-display").text("%s");' 
          . '});' 
          . '</script>' 
          , $state['label'] 
        ); 
       } 
      } 
     }); 


     add_action('admin_footer-edit.php', function() use($states, $postType) { 

      global $post; 

      if (!$post || $post->post_type !== $postType) { 
       return false; 
      } 

      foreach ($states as $id => $state) { 
       printf(
         '<script>' 
         . 'jQuery(document).ready(function($){' 
         . " $('select[name=\"_status\"]').append('<option value=\"%s\">%s</option>');" 
         . '});' 
         . '</script>' 
         , $id 
         , $state['label'] 
       ); 
      } 
     }); 

     add_filter('display_post_states', function($states, $post) use($states, $postType) { 

      foreach ($states as $id => $state) { 
       if ($post->post_type == $postType && $post->post_status === $id) { 
        return array($state['label']); 
       } else { 
        if (array_key_exists($id, $states)) { 
         unset($states[$id]); 
        } 
       } 
      } 

      return $states; 
     }, 10, 2); 
    } 

} 

這裏如何使用

add_action('init', function() { 
    PostStatusExtender::extend(self::NAME, array(
     'sold' => array(
      'label' => __('Sold', 'viasit'), 
      'public' => true, 
      'exclude_from_search' => true, 
      'show_in_admin_all_list' => true, 
      'show_in_admin_status_list' => true, 
      'label_count' => _n_noop('Sold <span class="count">(%s)</span>', 'Sold <span class="count">(%s)</span>'), 
     ) 
    )); 
}); 

編輯:在代碼固定錯字。

+0

這只是給了我一個白屏,並沒有做到...... – Justin 2017-06-16 21:08:00

相關問題