2015-05-11 32 views
-1

不同的扉頁上不同的內容我有一個WordPress的模板,我想顯示對不同類別的網頁不同的內容:如何顯示在WordPress

single_cat_title(''); 

如何在PHP模板使用它?當我有一個類別(例如CATEGORY-1)時,我需要顯示與其他類別不同的​​內容(例如CATEGORY-2)。

if single_cat_title(''); is a CATEGORY-1 then display CONTENT-1 else DISPLAY CONTENT-1 
+1

閱讀有關[IF](http://php.net/manual/en/control-structures.if.php)/ [其他](HTTP:// php.net/manual/en/control-structures.elseif.php)/[switch](http://php.net/manual/en/control-structures.switch.php)聲明?你有沒有試圖自己做任何事情? – George

+0

我會建議PHP的第一個非常基本的初學者介紹。 **如果single_cat_title('');是CATEGORY-1然後顯示CONTENT-1否則DISPLAY CONTENT-1 **不是任何PHP-ish。 – ggdx

回答

0

我能想出兩個解決方案。

  1. 您可以使用category templates。他們背後的想法是,你爲每個類別創建一個單獨的PHP文件。對於類別ID,您可以使用category-1.php,category-2.php等名稱,對於類別名稱可以使用category-name.php,category-other-name.php等。 爲避免在每個文件中寫入相同的代碼段,您應該將共享代碼放在另一個文件或多個文件中(例如shared.php或甚至更好的header.php,footer.php等)。您可以將共享代碼包裝在functions中以獲得更多控制。然後包括在所有類別模板共享文件(S)和調用功能:

    include('shared.php'); // including the shared code 
    show_header(); // our function for displaying header 
    /* 
    * do category-specific stuff 
    */ 
    show_footer(); // our function for displaying footer 
    
  2. 您可以使用一個類模板(category.php默認情況下),檢查什麼是當前的類別並執行不同的不同的動作類別。有一個檢查類別的特殊功能,稱爲is_category()。它應該是這樣的:

    //(...) some header code here 
    
    if(is_category('some_category')) { 
         // perform specific actions 
    } 
    else if(is_category('other_category')) { 
         // perform specific actions 
    } 
    else { 
         // some unknown category. perform specific actions 
    } 
    
    // (...) some footer code here