简体   繁体   English

Word语句中的Wordpress / PHP通配符

[英]Wordpress / PHP wildcard in if statement

I have this line of code that works perfectly: 我有这行代码可以完美地工作:

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

Except, I have a large number of categories and want to use a "wildcard" for the name of the category and have the header switch based on finding only a few characters in the category name. 除此之外,我有很多类别,并且想要对类别名称使用“通配符”,并基于仅在类别名称中找到几个字符的标题切换。

Something more like: 更像是:

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

Categories are stored in the database so you will need something like: 类别存储在数据库中,因此您将需要以下内容:

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

This should help you get the name of the page you are accessing at page load. 这应该可以帮助您获取页面加载时正在访问的页面的名称。 Then you can do something like: 然后,您可以执行以下操作:

get_header($cat_name); get_header($ cat_name);

EDIT : 编辑:

Since you are looking just for a suffix, the best way to do this is either with something like strripos or a regular expression matching in an if/else structure or a switch statement depending on your needs: 由于您只是在寻找后缀,因此最好的方法是使用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