WordPress.org

Ready to get started?Download WordPress

Forums

php relational operators : several values after != (4 posts)

  1. Manuel Schmalstieg
    Member
    Posted 3 years ago #

    My php knowledge being limited, I need some help in order to customize the following bit of code, which is used in a sidebar that displays neighbouring posts of the same category.

    if (($all_cats_of_post[$i]->cat_ID) != 8 )

    The number 8 is the category ID that gets excluded from the sidebar listing. It works fine, but now I need to exclude not only ONE but SEVERAL categories, say cat_ID 10 and cat_ID 13.

    How can I wrap several numbers inside this conditional statement? My search through PHP manual pages doesn't bring up any example where a relational operator (==, !=) is followed by a series of optional values.

  2. Manuel Schmalstieg
    Member
    Posted 3 years ago #

    for the record, i am aware that this form is possible:

    if ((($all_cats_of_post[$i]->cat_ID) != 8 ) && (($all_cats_of_post[$i]->cat_ID) != 10 ) && (($all_cats_of_post[$i]->cat_ID) != 13 ))

    but I hope that there's a less redundant way of formulating the same statement ...

  3. DigitalSquid
    Member
    Posted 3 years ago #

    Maybe use an array instead:

    $categories = array(8, 10, 13);
    if(!in_array($all_cats_of_post[$i]->cat_ID, $categories)) {
        //Do something
    }
  4. Manuel Schmalstieg
    Member
    Posted 3 years ago #

    Yes, that's exactly what I was looking for.
    So simple.
    Thanks a lot!

Topic Closed

This topic has been closed to new replies.

About this Topic