0

I am trying to set up my site so that my WooCommerce product categories only show images on my home page. Currently I have this:

<?php
function fp_categories() {
    if( is_front_page() ) {
        add_action( 'woocommerce_before_subcategory_title', 'woocommerce_subcategory_thumbnail', 10 ) ;
    } else {
        remove_action( 'woocommerce_before_subcategory_title', 'woocommerce_subcategory_thumbnail', 10 ) ;
    }
}
?>

This does remove the images, but it does so from every page. I've tried using is_home instead of is_front_page, but it didn't help either. Any suggestions?

2
  • You probably mean is_shop() to check if current page is store's main page. Here you can find other WooCommerce conditional tags.
    – nmr
    Jul 23, 2019 at 13:00
  • Unfortunately, that didn't seem to do it either. I'm going to look through the conditional tags to see if I come across anything. Thanks for the link.
    – Saenen
    Jul 23, 2019 at 13:34

3 Answers 3

0

Try running your function hooked into the template_redirect action like so:

<?php
function fp_categories() {
    if( is_front_page() ) {
        add_action( 'woocommerce_before_subcategory_title', 'woocommerce_subcategory_thumbnail', 10 ) ;
    } else {
        remove_action( 'woocommerce_before_subcategory_title', 'woocommerce_subcategory_thumbnail', 10 ) ;
    }
}

add_action( 'template_redirect', 'fp_categories' );
?>

I'm fuzzy on the logic why this works - I believe otherwise the functions are running before they're available, but hopefully, someone can further clarify.

2
  • This did it. Guess I'll have to do more research into template_redirect. Thanks!
    – Saenen
    Jul 23, 2019 at 17:08
  • Down voter, explain your down vote.
    – disinfor
    Jul 23, 2019 at 18:24
0

Please try this is_home() also like:

if( is_home() || is_front_page() )
2
  • This didn't seem to do the trick. I'll see if any other operators do though. Thanks!
    – Saenen
    Jul 23, 2019 at 13:51
  • @Saenen You can also use this is_page( ). just passing the page name or page id inside this function. Jul 23, 2019 at 14:05
0

I will propose a different way, changing the list of hooked functions (adding, deleting) at the beginning of the action.

add_action( 'woocommerce_before_subcategory_title', 'fp_categories', 1 );
function fp_categories()
{
    if ( ! is_shop() && ! is_front_page() )
        remove_action('woocommerce_before_subcategory_title', 'woocommerce_subcategory_thumbnail', 10);
    else
        add_action('woocommerce_before_subcategory_title', 'woocommerce_subcategory_thumbnail', 10);
}

In this particular case, using above method or accepted answer does not make a difference.

If condition will be based on some product feature (eg. meta field, owned tags), this method will allow to add or remove hooked functions for each product (condition checked at each execution).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.