最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to check if the postID is in an array?

programmeradmin3浏览0评论
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
}
发布评论

评论列表(0)

  1. 暂无评论