0

I have a really simple question about PHP conditional logic. I have some code where I want to load a different header file based on the post category type. The code below works:

if(in_category('news')) {
    get_header('tertiary');
}
elseif(in_category('events')) {
    get_header('tertiary');
}
else {
    get_header('secondary');
}

But when I try to simplify the code to:

if(in_category('news' || 'events)) {
    get_header('tertiary');
}
else {
    get_header('secondary');
}

The tertiary header file for the events is not loading, it is showing the secondary header file. I'm using similar code elsewhere in my theme and it is working with no issues. So I'm not sure why it is not working here. I'm not getting an errors in my PHP console.

1
  • 3
    Without looking up any docs on that method, are you sure thats even valid that you can throw a condition statement inside of a method call? Wouldn't it be more like in_category('news') || in_category('events') Commented Aug 28, 2017 at 15:16

3 Answers 3

3

There may be two ways you can do this. Check in_category individually (this will always work)

if(in_category('news') || in_category('events')) {

Or in_category can take an array, in some versions of Wordpress:

if(in_category(['news', 'events'])) {
Sign up to request clarification or add additional context in comments.

Comments

3

You need to read the documentation and instead of

 if(in_category('news' || 'events)) {
 ...

do

if(in_category(['news', 'events'])) {
...

Comments

1

You need to read the documentation

 if ( in_category( Array( 'news', 'events' ) ) ) {
     get_header('tertiary');
 } else {
     get_header('secondary');
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.