简体   繁体   中英

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

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);

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:

//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(...){
   ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM