2012-11-21 24 views
0

我需要用我的index.php文件爲我的標準的WordPress的新聞,同時也爲自定義後的類型。WordPress的 - 使用的index.php爲崗位和自定義文章類型

在我的index.php文件中我使用的是標準的循環不帶任何參數或參數。

我試圖使其與行動「pre_get_posts」工作。我一直在嘗試更改查詢帖子類型,但仍然無效。

$query->set('post_type', 'any') 

所以我需要/卷宗的方式/使用的index.php這意味着我需要改變循環的查詢時/卷宗中的URL檢測/。

非常感謝!

編輯:

這裏是我與 「pre_get_post」 調用該函數:

function custom_post_types($query) { 
    if($query->is_main_query() && $query->query['name'] == 'dossiers') { 
     $query->set('post_type', 'any'); 

     var_dump($query); 
    } 
} 

這裏是$查詢的var_dump:

object(WP_Query)#100 (42) { 
    ["query"]=> 
    array(2) { 
    ["page"]=> 
    string(0) "" 
    ["name"]=> 
    string(8) "dossiers" 
} 
["query_vars"]=> 
array(46) { 
    ["page"]=> 
    string(0) "" 
    ["name"]=> 
    string(8) "dossiers" 
    ["error"]=> 
    string(0) "" 
    ["m"]=> 
    int(0) 
    ["p"]=> 
    int(0) 
    ["post_parent"]=> 
    string(0) "" 
    ["subpost"]=> 
    string(0) "" 
    ["subpost_id"]=> 
    string(0) "" 
    ["attachment"]=> 
    string(0) "" 
    ["attachment_id"]=> 
    int(0) 
    ["static"]=> 
    string(0) "" 
    ["pagename"]=> 
    string(0) "" 
    ["page_id"]=> 
    int(0) 
    ["second"]=> 
    string(0) "" 
    ["minute"]=> 
    string(0) "" 
    ["hour"]=> 
    string(0) "" 
    ["day"]=> 
    int(0) 
    ["monthnum"]=> 
    int(0) 
    ["year"]=> 
    int(0) 
    ["w"]=> 
    int(0) 
    ["category_name"]=> 
    string(0) "" 
    ["tag"]=> 
    string(0) "" 
    ["cat"]=> 
    string(0) "" 
    ["tag_id"]=> 
    string(0) "" 
    ["author_name"]=> 
    string(0) "" 
    ["feed"]=> 
    string(0) "" 
    ["tb"]=> 
    string(0) "" 
    ["paged"]=> 
    int(0) 
    ["comments_popup"]=> 
    string(0) "" 
    ["meta_key"]=> 
    string(0) "" 
    ["meta_value"]=> 
    string(0) "" 
    ["preview"]=> 
    string(0) "" 
    ["s"]=> 
    string(0) "" 
    ["sentence"]=> 
    string(0) "" 
    ["fields"]=> 
    string(0) "" 
    ["category__in"]=> 
    array(0) { 
    } 
    ["category__not_in"]=> 
    array(0) { 
    } 
    ["category__and"]=> 
    array(0) { 
    } 
    ["post__in"]=> 
    array(0) { 
    } 
    ["post__not_in"]=> 
    array(0) { 
    } 
    ["tag__in"]=> 
    array(0) { 
    } 
    ["tag__not_in"]=> 
    array(0) { 
    } 
    ["tag__and"]=> 
    array(0) { 
    } 
    ["tag_slug__in"]=> 
    array(0) { 
    } 
    ["tag_slug__and"]=> 
    array(0) { 
    } 
    ["post_type"]=> 
    string(3) "any" 
} 
    ["tax_query"]=> 
    NULL 
    ["meta_query"]=> 
    bool(false) 
    ["post_count"]=> 
    int(0) 
    ["current_post"]=> 
    int(-1) 
    ["in_the_loop"]=> 
    bool(false) 
    ["comment_count"]=> 
    int(0) 
    ["current_comment"]=> 
    int(-1) 
    ["found_posts"]=> 
    int(0) 
    ["max_num_pages"]=> 
    int(0) 
    ["max_num_comment_pages"]=> 
    int(0) 
    ["is_single"]=> 
    bool(true) 
    ["is_preview"]=> 
    bool(false) 
    ["is_page"]=> 
    bool(false) 
    ["is_archive"]=> 
    bool(false) 
    ["is_date"]=> 
    bool(false) 
    ["is_year"]=> 
    bool(false) 
    ["is_month"]=> 
    bool(false) 
    ["is_day"]=> 
    bool(false) 
    ["is_time"]=> 
    bool(false) 
    ["is_author"]=> 
    bool(false) 
    ["is_category"]=> 
    bool(false) 
    ["is_tag"]=> 
    bool(false) 
    ["is_tax"]=> 
    bool(false) 
    ["is_search"]=> 
    bool(false) 
    ["is_feed"]=> 
    bool(false) 
    ["is_comment_feed"]=> 
    bool(false) 
    ["is_trackback"]=> 
    bool(false) 
    ["is_home"]=> 
    bool(false) 
    ["is_404"]=> 
    bool(false) 
    ["is_comments_popup"]=> 
    bool(false) 
    ["is_paged"]=> 
    bool(false) 
    ["is_admin"]=> 
    bool(false) 
    ["is_attachment"]=> 
    bool(false) 
    ["is_singular"]=> 
    bool(true) 
    ["is_robots"]=> 
    bool(false) 
    ["is_posts_page"]=> 
    bool(false) 
    ["is_post_type_archive"]=> 
    bool(false) 
    ["query_vars_hash"]=> 
    string(32) "3cd80adcb57d626df6535f6dafb98057" 
    ["query_vars_changed"]=> 
    bool(false) 
    ["thumbnails_cached"]=> 
    bool(false) 
} 

的 「名稱」 的價值被設置爲「檔案」,這應該是一個帖子名稱的slu if,如果我將它設置爲「」,我會用我最後一個自定義帖子類型重定向到single.php。

+0

爲什麼你不能使用'single- {custom-post-type} .php'模板文件? WordPress的層次結構工作方式index.php將永遠是最後被調用的模板。 – Jrod

+0

因爲它會成爲索引的精確副本。 – Khealdor

回答

0

我懷疑你的條件不與$query對象正確交互。

嘗試:

​​3210

如果不工作,也許這將工作:

$query_var_name = get_query_var('name'); 
if($query->is_main_query() && $query_var_name == 'dossiers') 

此外,添加某種測試,以確保您的index.php實際上是模板文件Wordpress在訪問/檔案時使用

+0

我的條件是工作,因爲: [「post_type」] =>串(3)「任何」 是我的var_dump – Khealdor

+0

這是因爲WordPress的查詢類是足夠聰明,你的'set'應用於適當的' query_var'。如果你想'名字',我敢肯定你必須從'query_vars'中得到它。 – crowjonah

+0

我的意思是...條件不是問題,如果我完全刪除它,當我去/卷宗/ – Khealdor

相關問題