-
-
Save twoelevenjay/a2195b052c4feb046d7f71fcae5c4d1b to your computer and use it in GitHub Desktop.
<?php | |
/** | |
* Move WooCommerce subcategory list items into | |
* their own <ul> separate from the product <ul>. | |
*/ | |
add_action( 'init', 'move_subcat_lis' ); | |
function move_subcat_lis() { | |
// Remove the subcat <li>s from the old location. | |
remove_filter( 'woocommerce_product_loop_start', 'woocommerce_maybe_show_product_subcategories' ); | |
add_action( 'woocommerce_before_shop_loop', 'msc_product_loop_start', 40 ); | |
add_action( 'woocommerce_before_shop_loop', 'msc_maybe_show_product_subcategories', 50 ); | |
add_action( 'woocommerce_before_shop_loop', 'msc_product_loop_end', 60 ); | |
} | |
/** | |
* Conditonally start the product loop with a <ul> contaner if subcats exist. | |
*/ | |
function msc_product_loop_start() { | |
$subcategories = woocommerce_maybe_show_product_subcategories(); | |
if ( $subcategories ) { | |
woocommerce_product_loop_start(); | |
} | |
} | |
/** | |
* Print the subcat <li>s in our new location. | |
*/ | |
function msc_maybe_show_product_subcategories() { | |
echo woocommerce_maybe_show_product_subcategories(); | |
} | |
/** | |
* Conditonally end the product loop with a </ul> if subcats exist. | |
*/ | |
function msc_product_loop_end() { | |
$subcategories = woocommerce_maybe_show_product_subcategories(); | |
if ( $subcategories ) { | |
woocommerce_product_loop_end(); | |
} | |
} |
Try to replace the end of the code with this:
/**
* Conditonally end the product loop with a </ul> if subcats exist.
*/
function msc_product_loop_end() {
$subcategories = woocommerce_maybe_show_product_subcategories();
if ( $subcategories ) {
woocommerce_product_loop_end();
}
//if category has products, show title PRODUCTS title
if (have_posts()){
echo do_shortcode('[title text="TUOTTEET" tag_name="h2"]');
}
}
Also, I added a condition that the word Products will show only when the category has some products. If you want to delete this condition, put echo do_shortcode('[title text="TUOTTEET" tag_name="h2"]');
outside have_posts condition...
Is it what you were trying to achieve?
Well I'll be damned, that sure did the trick! This was exactly what I was trying to achieve, thank you so much! :)
Glad to help sir :)
@anddilolz and @fkoomek, you two are awesome!
@twoelevenjay @fkoomek mad props to you both!
@twoelevenjay first o all, thank you for sharing with us, your code above. I use is and it is very helpful !
Is it also possible, combining the code above to align all categories and products to center?
ie the shop page has 4 columns and there is only 2 subcategories, can be center aligned ?
or a subcategory has only 3 products, can also appeared centered ?
kind regards
e.a
@EchoA331 It Is possible, you have to use CSS for that.
thanks for your reply, unfortunately I do not know how !
I found many similar examples on the web, but none was working. Thanks anyway ! Have a nice day !
Do you have a link to your site?
@EchoA331 Try to add this to your custom CSS code:
#main .products.row { justify-content: center; }
Here's the current code: