user3332898
user3332898

Reputation: 17

(WordPress) How to use if OR if post?

I want to show some HTML code for specific posts, so I put this on HEADER templete:

<?php if(is_single(24) || is_single(34)) { ?>
MY HTML CODES
<?php } ?>

I tried those too:

<?php if(is_single(24) && is_single(34)) { ?>
<?php if((is_single(24)) && (is_single(34))) { ?>
<?php if((is_single(24)) || (is_single(34))) { ?>

And its not working. If I put this code for single post, like this:

<?php if(is_single(24) { ?>

Its working well.. but I need to do that for many posts.

Upvotes: 1

Views: 64

Answers (2)

mleko
mleko

Reputation: 12243

Try using in_array

<?php if(in_array(POST_ID, array(POST_IDS))) { ?>
MY HTML CODES
<?php } ?>

Upvotes: 0

Chelsea Urquhart
Chelsea Urquhart

Reputation: 1428

You could do if (is_single() && in_array($post->ID, array(24, 34)) {}

Depending on context, you may need to make $post global.

I'd probably add some metadata to the posts though and check for that instead... Less messy.

Upvotes: 2

Related Questions