Justin Seidl
Justin Seidl

Reputation: 249

What's wrong with my array for Wordpress?

I'm trying to exclude certain posts in Wordpress using an array. I can successfully remove one of the posts by doing this:

<?php if ( $post->ID != '443' ) { ?>
  ...
<?php } ?>

I'm trying to create an array of posts that I want to remove and have this:

<?php $excluded = array('443', '479', '464', '589', '333', '296', '381', '405', '252', '301', '291', '306', '632', '634', '636', '313', '317', '389', '410', '417', '321');
if ( $post->ID != $exclude ) { ?>
  ...
<?php } ?>

I've been trying to teach myself PHP and am struggling with arrays, any help?

Upvotes: 0

Views: 210

Answers (3)

jrq
jrq

Reputation: 31

you should use in_array to test if a value is in an array

ie,

if (in_array($post->ID, $exclude)) {

}

Upvotes: 0

matthewvb
matthewvb

Reputation: 942

The problem with your if statement is you're checking if the postID != the entire array, not if a value is inside that array which matches the value.

You want to use the in_array function. That would look something like:

if ( !in_array($post->ID, $exclude )

Upvotes: 0

Stefan Dochow
Stefan Dochow

Reputation: 1454

Comparing a number with a number does work, as your first example shows.

But im the second one you compare a number (ID) with an array (a list of numbers).

To check, wether the ID is an element of your "excluded" list, you will have to use in_array():

if (!in_array($post->ID,$exclude)){
...
}

Regards,

STEFAN

Upvotes: 2

Related Questions