2016-03-30 83 views
0

我正在使用wordpress模板。我有很多類別,如冒險,體育,新聞。現在,每個類別都有帖子,我知道singlep.php正在生成所有類型的帖子。假設,如果您點擊新聞,它會顯示與新聞相關的所有帖子。每個帖子被標識爲相同的頁面類型「發佈」。但是,當單擊Adventure時,我想調用單獨的Post php文件而不是single.php。意思是,當我瀏覽Adventure類別的帖子時,它應該來自example.php。 Example.php是我想要與Adventure鏈接的文件。我需要幫助。它會像,在Wordpress中爲每個類別的帖子創建不同的頁面類型

if (Category == Adventure) 
call the example.php 
else 
single.php 

我應該在哪裏專注於做到這一點。有沒有什麼和functions.php有關?另外,wordpress如何調用Single.php?所以我可以指示它也打電話給我的example.php

回答

0

你試過編輯你的single.php嗎?從WordPress網站本身

if (in_category('fruit')) { 
    include 'single-fruit.php'; 
} elseif (in_category('vegetables')) { 
    include 'single-vegetables.php'; 
} else { 
    // Continue with normal Loop 
    if (have_posts()) : while (have_posts()) : the_post(); 
    // ... 
} 

in_category

in_category (int|string|array $category, int|object $post = null) 

樣品。這也可以作爲參考,模板級別:single post


這可以通過添加自定義頭文件和編輯的single.php文件

在最頂部到自定義每個類別單篇文章標題中的選項的single.php文件

if(in_category('apple')){ 
    get_header('fruits'); 
}elseif(in_category('cabbage')){ 
    get_header('vegetables'); 
}else{ 
    get_header(); 
} 

然後用確切的名稱上面的示例代碼header-fruits.php & header-vegetables.php

創建自己的自定義頁眉
+0

我不想編輯single.php。而是想創建一個類似的文件example.php。因爲,我想在標題中爲特定類別的每個帖子添加一些代碼,而其他類別的帖子將與之前從single.php中調用的相同。 – Codelife

+0

我需要一個小小的幫助,它存在於哪個文件中: - if(have_posts()):while(have_posts()):the_post(); – Codelife

+0

@Codelife更新了答案,那是你在找什麼? – Chay22

相關問題