Advertisement
Misplon

Polestar Display WooCommerce category image

Dec 6th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.33 KB | None | 0 0
  1. /**
  2.  * Display WooCommerce category image on category archive page.
  3.  */
  4. function polestar_child_woocommerce_category_image() {
  5.     if ( function_exists( 'is_woocommerce' ) && is_product_category() ) {
  6.         global $wp_query;
  7.         $cat = $wp_query->get_queried_object();
  8.         $thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
  9.         $image = wp_get_attachment_url( $thumbnail_id );
  10.         if ( $image ) {
  11.             echo '<div class="polestar-wc-category-banner" style="background: url(' . $image . ') no-repeat; background-size: cover; height: 200px; margin-bottom: 30px; width: 100%;"></div>';
  12.         }
  13.     }
  14. }
  15. add_action( 'polestar_content_before', 'polestar_child_woocommerce_category_image', 2 );
  16.  
  17. /**
  18.  * Adds child theme custom classes to the array of body classes.
  19.  *
  20.  * @param array $classes Classes for the body element.
  21.  * @return array
  22.  */
  23. function polestar_child_body_classes( $classes ) {
  24.  
  25.     // Header margin.
  26.     if ( function_exists( 'is_woocommerce' ) && is_product_category() ) {
  27.         global $wp_query;
  28.         $cat = $wp_query->get_queried_object();
  29.         $thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
  30.         $image = wp_get_attachment_url( $thumbnail_id );
  31.         if ( $image ) {    
  32.             $classes[] = 'no-header-margin';
  33.         }
  34.         return $classes;
  35.     }
  36. }
  37. add_filter( 'body_class', 'polestar_child_body_classes' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement