John Davidson

javascript - Show this custom text in one specific category

0 comments
Message:


How to alter this custom text, so it will only be shown in one specific category, let's say: ONLY in category: tables?


add_action( 'woocommerce_after_shop_loop_item', 'show_meer_varianten_loop', 100 );
function show_meer_varianten_loop() { ?>
<div class="custom-label-variable">✓ Meer varianten</div>
<?php
}

in css:


.products .product:not(.product-type-variable) .custom-label-variable {
display: none;
}


My Answer:
To alter the custom text so it will only be shown in the "tables" category, you can add a conditional statement inside the show_meer_varianten_loop function to check if the current product is in the "tables" category. Here's an example of how you can modify the function:

javascript
add_action( 'woocommerce_after_shop_loop_item', 'show_meer_varianten_loop', 100 );

function show_meer_varianten_loop() {
global $product;

// Check if the product is in the "tables" category
if ( has_term( 'tables', 'product_cat', $product->get_id() ) ) {
echo 'Custom text for tables category';
}
}


In this code snippet, we use the has_term function to check if the current product has the "tables" category assigned to it. If it does, the custom text will be displayed. Otherwise, the text will not be shown.

Rate this post

4 of 5 based on 3483 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us