How to check if the postID is in an array?

Closed. This question is off-topic. It is not currently accepting answers.Your question should be specific to WordPress.

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 5 years ago.

Improve this question

In Wordpress, I am trying to check through an if else statement if the post is of a certain ID as follows:

<?php if ($post->ID == array(224,222,583,645,203,11,639,228,226,230,634,615,625,214,220,194)) : ?>
    ...do something...
<?php else : ?>
    ...do nothing...
<?php endif; ?>

This isn't working. Can you please help in knowing how to use the check the post ID in arrays?

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 5 years ago.

Improve this question

In Wordpress, I am trying to check through an if else statement if the post is of a certain ID as follows:

<?php if ($post->ID == array(224,222,583,645,203,11,639,228,226,230,634,615,625,214,220,194)) : ?>
    ...do something...
<?php else : ?>
    ...do nothing...
<?php endif; ?>

This isn't working. Can you please help in knowing how to use the check the post ID in arrays?

Share Improve this question asked Jun 13, 2019 at 8:07 Keyur AminKeyur Amin 31 silver badge4 bronze badges 3
  • See in_array(). – Sally CJ Commented Jun 13, 2019 at 8:16
  • Did this but didn;t work $post = $wp_query->post; if (in_array($post->ID,array(224,222,583,645,203,11,639,228,226,230,634,615,625,214,220,194))) : ?> – Keyur Amin Commented Jun 13, 2019 at 8:28
  • This is essentially off-topic and will likely be closed, but I quickly posted the in_array() solution. – Christine Cooper Commented Jun 13, 2019 at 8:43
Add a comment  | 

1 Answer 1

Reset to default 2

As Sally highlights, you can use in_array() like this (untested):

// target ids
$ids = array(123, 321, 213);

if (!empty($post->ID) && is_numeric($post->ID) && in_array((int)$post->ID, $ids)) {
   // do jazz
} else {
   // do stuff
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745406080a4626320.html

相关推荐

  • How to check if the postID is in an array?

    Closed. This question is off-topic. It is not currently accepting answers.Your question should be specific to WordPress.

    11小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信