• Resolved yollyflopp

    (@yollyflopp)


    I’ve inherited a page from another developer, the page is built back in 2013 and the customer just want me to do some quickfixes on it.

    The wanted me to split Seasonal products into Easter- and Christmasproducts, I’ve made that split but on the pages that show regular products (they shouldn’t show Easter nor Christmas) still show them and I need to know how to exclude both from the regular products-pages. Here’s what I’ve got to work with, it’s not that pretty written.

    The former developer defined the Seasonal products (SASONGSPRODUKTER) via functions.php

    define('SASONGSPRODUKTER_TERM_ID', 12);

    And then excluded SASONGSPRODUKTER in the template for regular products with:

    if (in_array($product_category->term_id, $usedCategories) && $product_category->term_id != SASONGSPRODUKTER_TERM_ID):

    And i added Christmas (JULPRODUKTER) and Easter (PASKPRODUKTER) to functions.php

    define('SASONGSPRODUKTER_TERM_ID', 12);
    define('JULSPRODUKTER_TERM_ID', 21);
    define('PASKPRODUKTER_TERM_ID', 22);

    If I change SASONGSPRODUKTER to either JULPRODUKTER or PASKPRODUKTER, like this:

    if (in_array($product_category->term_id, $usedCategories) && $product_category->term_id != PASKPRODUKTER_TERM_ID):

    I exclude Christmas OR Easter, so I know that works.

    How do I either define SASONGSPRODUKTER to be 21 AND 22 instead of 12 (it doesn’t work with 21, 22)? Or add PASKPRODUKTER and JULPRODUKTER to the “if (in_array…”?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    You can assign arrays to constants, but in your case that defeats the reason for constants in the first place — maintainability should the IDs change. You should make the array of IDs out of the individual constants. Of course using the != operator with an integer against an array will always return true no matter what. Instead, use the in_array() function to evaluate your exclusion conditional, much like it is used to evaluate the used categories, except that in_array() is negated:

    if ( in_array( $product_category->term_id, $usedCategories )
      && ! in_array( $product_category->term_id, array(
        JULSPRODUKTER_TERM_ID,
        PASKPRODUKTER_TERM_ID,
      ))):

    This returns true if the ID is used AND the ID is not 21 or 22. Clearly you can easily exclude other IDs in the future by simply adding them to the array.

    Thread Starter yollyflopp

    (@yollyflopp)

    Thanks, that did the trick! And as you said, easy to exclude another ID if needed.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Exclude term_id’ is closed to new replies.