2010-09-07 70 views
0
$this->add_meta_box('select_post_template', __('Post Template', 'custom-post-templates'), 'select_post_template', 'post', 'side', 'default'); 

要使插件與自定義帖子類型一起工作,我被告知將「帖子」更改爲自定義帖子類型的名稱。有誰知道我是否可以通過改變這條線以某種方式使它與全部定製的帖子類型(包括常規帖子)一起工作?提前 http://wordpress.org/extend/plugins/custom-post-template/如何包含所有自定義帖子類型而不是僅帖子

感謝:

僅供參考,我發現這個在: http://wordpress.org/support/topic/custom-post-templates-with-custom-post-types-in-wp-30?replies=5#post-1679398

而且這是在參考了自定義帖子模板插件!

編輯:

我已經試過:

$post_types = get_post_types(array("public" => true)); 
foreach ($post_types as $post_type) { 
    $this->add_meta_box("select_post_template", __("Post Template", "custom-post-templates"), "select_post_template", $post_type, "side", "default"); 
} 

但自定義文章類型仍然沒有得到模板選擇菜單。這些帖子就像他們對原始代碼所做的一樣。感謝您的建議...有沒有人有另一個?

注意:從概念上講,該方法是可靠的。如果我使用自定義帖子類型的列表創建自己的數組,則此代碼確實將模板添加到它們。

回答

1

您可以遍歷所有已註冊的帖子類型併爲每個添加元框,但您可能需要過濾掉某些類型,因爲附件也是帖子。

$post_types = get_post_types(array("public" => true)); 
foreach ($post_types as $post_type) { 
    add_meta_box("select_post_template", __("Post Template", "custom-post-templates"), "select_post_template", $post_type, "side", "default"); 
} 

具體就自定義帖子模板插件的問候,我認爲這個問題是它的初始化後您的自定義文章類型註冊(因爲它不使用掛鉤)。因此,$post_types(上面)不包含您的類型,並且不能爲他們添加元框。你可以嘗試加入這個技巧(在custom-post-templates.php末):

add_action('init', 'hack_add_meta_boxes'); 
function hack_add_meta_boxes() { 
    global $CustomPostTemplates; 
    $post_types = get_post_types(array('public' => true)); 
    foreach ($post_types as $post_type) { 
    $CustomPostTemplates->add_meta_box('select_post_template', __('Post Template', 'custom-post-templates'), 'select_post_template', $post_type, 'side', 'default'); 
    } 
} 
+0

感謝您嘗試,但它並沒有擴展到自定義文章類型:( – Matrym 2010-09-09 20:14:59

+0

我認爲你的問題可能是您的自定義職位類型在自定義帖子模板插件被初始化之後被註冊(因爲它不使用鉤子),所以'$ post_types'不包含你的帖子類型,並且不爲它們添加元框。 – 2010-09-09 21:28:18

相關問題