WordPress.org

Support

Support » Themes and Templates » php relational operators : several values after !=

php relational operators : several values after !=

  • 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.

Viewing 3 replies - 1 through 3 (of 3 total)
  • 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 …

    DigitalSquid

    @twelvefootsnowman

    Maybe use an array instead:

    $categories = array(8, 10, 13);
    if(!in_array($all_cats_of_post[$i]->cat_ID, $categories)) {
        //Do something
    }

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

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘php relational operators : several values after !=’ is closed to new replies.
Skip to toolbar