Created
April 20, 2017 16:53
-
-
Save gabelloyd/73e2b47bdb7e6b0aae13de59b95d0dd1 to your computer and use it in GitHub Desktop.
Display WooCommerce featured images on archive-product.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if ( ! defined( 'ABSPATH' ) ) { | |
exit; // Exit if accessed directly | |
} | |
get_header( 'shop' ); ?> | |
<?php | |
/** | |
* woocommerce_before_main_content hook. | |
* | |
* @hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content) | |
* @hooked woocommerce_breadcrumb - 20 | |
*/ | |
do_action( 'woocommerce_before_main_content' ); | |
?> | |
<?php if ( apply_filters( 'woocommerce_show_page_title', true ) ) : ?> | |
<div class="wc-archive-hero-wrapper full-width" <?php woocommerce_category_featured_image(); ?>> | |
<h1 class="page-title"><?php woocommerce_page_title(); ?></h1> | |
<?php | |
/** | |
* woocommerce_archive_description hook. | |
* | |
* @hooked woocommerce_taxonomy_archive_description - 10 | |
* @hooked woocommerce_product_archive_description - 10 | |
*/ | |
do_action( 'woocommerce_archive_description' ); | |
?> | |
</div> | |
<?php endif; ?> | |
<?php if ( have_posts() ) : ?> | |
<?php | |
/** | |
* woocommerce_before_shop_loop hook. | |
* | |
* @hooked woocommerce_result_count - 20 | |
* @hooked woocommerce_catalog_ordering - 30 | |
*/ | |
do_action( 'woocommerce_before_shop_loop' ); | |
?> | |
<?php woocommerce_product_loop_start(); ?> | |
<?php woocommerce_product_subcategories(); ?> | |
<?php while ( have_posts() ) : the_post(); ?> | |
<?php wc_get_template_part( 'content', 'product' ); ?> | |
<?php endwhile; // end of the loop. ?> | |
<?php woocommerce_product_loop_end(); ?> | |
<?php | |
/** | |
* woocommerce_after_shop_loop hook. | |
* | |
* @hooked woocommerce_pagination - 10 | |
*/ | |
do_action( 'woocommerce_after_shop_loop' ); | |
?> | |
<?php elseif ( ! woocommerce_product_subcategories( array( 'before' => woocommerce_product_loop_start( false ), 'after' => woocommerce_product_loop_end( false ) ) ) ) : ?> | |
<?php wc_get_template( 'loop/no-products-found.php' ); ?> | |
<?php endif; ?> | |
<?php | |
/** | |
* woocommerce_after_main_content hook. | |
* | |
* @hooked woocommerce_output_content_wrapper_end - 10 (outputs closing divs for the content) | |
*/ | |
do_action( 'woocommerce_after_main_content' ); | |
?> | |
<?php | |
/** | |
* woocommerce_sidebar hook. | |
* | |
* @hooked woocommerce_get_sidebar - 10 | |
*/ | |
do_action( 'woocommerce_sidebar' ); | |
?> | |
<?php get_footer( 'shop' ); ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* woocommerce_category_featured_image function | |
* | |
* Give me a category thumbnail image. Used in the archive page as the header | |
* https://docs.woocommerce.com/document/woocommerce-display-category-image-on-category-archive/ | |
* http://wordpress.stackexchange.com/questions/200170/woocommerce-get-category-image-full-size | |
* | |
* Compatible with WooCommerce 3.0.2 | |
* | |
* Out put a header image for the categories with a fallback to a featured image. | |
* Featured image seems to only be necessary for the /shop base page. | |
* | |
* @access public | |
* @author gabelloyd | |
* @since 1.0 | |
* @return void | |
*/ | |
// check for plugin using plugin name | |
function set_image_size() { | |
/** | |
* Detect plugin. For use on Front End only. | |
*/ | |
include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); | |
if ( is_plugin_active( 'mobble/mobble.php' ) ) { | |
if(is_mobile()){ | |
$size = 'large'; | |
} elseif (is_tablet()) { | |
$size = 'fixed-height-gallery'; | |
} else { | |
$size = 'full'; | |
} | |
return $size; | |
} else { | |
$size = 'full'; | |
} | |
} | |
// This is output on the archive-product.php line 37 | |
function woocommerce_category_featured_image() { | |
if ( is_product_category() ){ | |
global $wp_query; | |
// Category Image | |
$size = set_image_size(); | |
// get the query object | |
$cat = $wp_query->get_queried_object(); | |
// get the thumbnail id using the queried category term_id | |
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true ); | |
// Get the attachment image information. Set the thumbnail size you want here. | |
$category_img = wp_get_attachment_image_src( $thumbnail_id, $size, false ); | |
if ( $category_img ) { | |
echo 'style="background-image: url(' . $category_img[0] . ');"'; | |
} | |
wp_reset_postdata(); | |
} elseif (is_shop()) { | |
// Help from https://gist.github.com/Bradley-D/7287723 | |
// Featured Image | |
// get the shop ID | |
$post_id = get_option( 'woocommerce_shop_page_id' ); | |
// Get the size we want | |
$size = set_image_size(); | |
// Set the url | |
$featured_image = get_the_post_thumbnail_url( $post_id, $size );; | |
if ( $featured_image ) { | |
echo 'style="background-image: url(' . $featured_image . ');"'; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello,
I want it to be like :
if a category has subcategories then display subcategories else display products. Can you please, help me.
Regards,
Usha