php - Make WooCommerce product categories only show images on homepage

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

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?

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?

Share Improve this question edited Jul 23, 2019 at 12:55 nmr 4,5672 gold badges17 silver badges25 bronze badges asked Jul 23, 2019 at 12:53 SaenenSaenen 32 bronze badges 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 Commented 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 Commented Jul 23, 2019 at 13:34
Add a comment  | 

3 Answers 3

Reset to default 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.

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745273541a4619897.html

相关推荐

  • php - Make WooCommerce product categories only show images on homepage

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

    1小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信