繁体   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