Pergunta

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?

Foi útil?

Solução

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.

Outras dicas

Please try this is_home() also like:

if( is_home() || is_front_page() )

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a wordpress.stackexchange
scroll top