• Hi.

    I want to do this but without the echo!!!!!:

    I want to be able to use straight html.

    <?php if ( is_product_category() ) {
    
      if ( is_product_category( 'shirts' ) ) {
        echo 'Hi! Take a look at our sweet tshirts below.';
      } elseif ( is_product_category( 'games' ) ) {
        echo 'Hi! Hungry for some gaming?';
      } else {
        echo 'Hi! Check our our products below.';
      }
    
    } ?>

    I want to do this:

    <?php if ( is_product_category() ) {
    
      if ( is_product_category( 'shirts' ) ) {
    my info
      } elseif ( is_product_category( 'games' ) ) {
    2nd info
      } else {
    general info
      }
    
    } ?>
Viewing 8 replies - 1 through 8 (of 8 total)
  • You need to use opening and closing PHP tags whenever you want to use HTML again:

    <?php if ( is_product_category() ) {
      if ( is_product_category( 'shirts' ) ) { ?>
        <p>my info</p>
      <?php  } elseif ( is_product_category( 'games' ) ) { ?>
        <p>2nd info</p>
      <?php } else { ?>
        <p>general info</p>
      <?php  }
    } ?>
    Thread Starter dmaeuk

    (@dmaeuk)

    Stephen, you are a Gentleman!

    Thank you so much works like a charm! Of course makes sense now.

    Thread Starter dmaeuk

    (@dmaeuk)

    Mmmmm i have come up against another problem, modifying the code to this!

    I have test as main woocommerce category and and all of the other test sub category also contains test in the slug, however the test.gif image shows up for some of the sub test categories but not others, im totally lost. It seems hit and miss yet all categories have test in slug.

    What am i missing?

    <?php if (has_term( 'test', 'product_cat' ) ) { ?>
        <img src="https://www.test.com/test.gif" />
    <?php } elseif (has_term( 'viking', 'product_cat' ) ) { ?>
        <img src="https://www.test.com/viking.gif" />
    <?php } else { ?>
    <?php } ?>

    IIRC, has_term() requires an exact match, so your first condition would match test, but not test-subcategory or protest. Can you clarify what you mean by “all of the other test sub category also contains test in the slug”? Are you using the same slug for all of your categories?

    Thread Starter dmaeuk

    (@dmaeuk)

    Yes, thank you.

    What i am simply trying to do which im finding hard in woocommerce is basically say;

    If in category test or its sub categories; do this

    what i dont want to have to do is create an array and list all of the slugs of test and its sub categories as this will take a long time and will require manual editing each time we add a sub category of test in the future.

    In the old days before woocommerce i would simply use something like;

    if_in_category(test); do this

    i just can not seem to find the conditional tag to make this happen

    You might be able to use get_term_children() for this purpose, but it would depend on how WooCommerce sets up the product_cat taxonomy. At this point, you would likely get better answers if you asked WooCommerce directly.

    Thread Starter dmaeuk

    (@dmaeuk)

    Thanks for you help but that doesnt work.

    95% of the time its working based on has_term, i do not get why the other 5% of the categories with the same term in the slug wont work!

    its really annoying, if it was black and white, either working or not working i can accept it but for the code to work on most sub categories witht he same slug term doesnt make any kind of sense to me! Weirdest thing ive come across,

    Getting a response from woocommerce is like waiting for xmas on December the 26th!

    Perhaps I’m misunderstanding what you’re trying to accomplish. I installed WooCommerce and set up one parent product category and two child product categories under the parent category. The slug for the parent category was “bedding” and the slugs for the child categories were “pillowcases” and “bedskirts”. I then created two fake products, one for each child category. Both products were assigned to the “bedding” parent category, and one product was assigned to the “pillowcases” category and the other product was assigned to the “bedskirts” category.

    With this code:

    <?php if ( has_term( 'bedding', 'product_cat' ) {
      <p>Hello, world!</p>
    <?php } ?>

    I see the phrase “Hello, world!” at the top of each of my product pages.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Conditional code.’ is closed to new replies.