簡體   English   中英

Word語句中的Wordpress / PHP通配符

[英]Wordpress / PHP wildcard in if statement

我有這行代碼可以完美地工作:

<?php if (in_category('CATEGORY-SUFFIX')) { get_header('SUFFIX'); } else { get_header(); } ?>

除此之外,我有很多類別,並且想要對類別名稱使用“通配符”,並基於僅在類別名稱中找到幾個字符的標題切換。

更像是:

<?php if (in_category('*SUFFIX')) { get_header('SUFFIX'); } else { get_header(); } ?>

類別存儲在數據庫中,因此您將需要以下內容:

https://codex.wordpress.org/Function_Reference/get_category

這應該可以幫助您獲取頁面加載時正在訪問的頁面的名稱。 然后,您可以執行以下操作:

get_header($ cat_name);

編輯:

由於您只是在尋找后綴,因此最好的方法是使用strripos之類的東西,或者根據需要在if / else結構或switch語句中匹配正則表達式:

//lets assume you want to catch all categories that end with ed (like teached)
if(strripos($cat_name,'ed', -2)!== FALSE){
   get_header('ed-header');
} elseif(...){
   ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM