• Resolved Evakos

    (@chrissyb)


    Hello everyone,

    Can someone make a suggestion on how to simply add the image caption to the product category image. I’m using this code from :

    add_action( 'woocommerce_archive_description', 'woocommerce_category_image', 2 );
    function woocommerce_category_image() {
        if ( is_product_category() ){
    	    global $wp_query;
    	    $cat = $wp_query->get_queried_object();
    	    $thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
    	    $image = wp_get_attachment_url( $thumbnail_id );
    	    if ( $image ) {
    		    echo '<img src="' . $image . '" alt="" />';
    		}
    	}
    }

    This would display it inside the post loop:

    <?php the_post_thumbnail();
    echo get_post(get_post_thumbnail_id())->post_excerpt; ?>

    Unfortunately I do not know how to add that to the function. Would it be better to modify the page that is outputting the product category information?

    Thanks whoever may take an interest 🙂

    Chris

    https://ww.wp.xz.cn/plugins/woocommerce/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Contributor Mike Jolley

    (@mikejolley)

    Where are you trying to add this, and how would it be displayed?

    If its on archives, why not use category description instead?

    Thread Starter Evakos

    (@chrissyb)

    Hi Mike,

    Thanks for replying – it was Easter here so sorry for the delay.

    I have text added via the category description, I just wanted to add a title to the image itself, just as you would via the editor.

    Does that make sense…?

    Chris

    Plugin Contributor Mike Jolley

    (@mikejolley)

    For the image itself (thumbnail) you can replace this function as it’s pluggable https://github.com/woothemes/woocommerce/blob/master/includes/wc-template-functions.php#L1679-L1698

    Thread Starter Evakos

    (@chrissyb)

    Hi Mike,

    Thanks could you elaborate a little 🙂

    Thanks

    Plugin Contributor Mike Jolley

    (@mikejolley)

    A pluggable function can be replaced by adding it to your theme functions.php. Your version will be used instead.

    Thread Starter Evakos

    (@chrissyb)

    Hi Mike i did this:

    add_action( 'woocommerce_archive_description', 'woocommerce_category_image', 2 );
    function woocommerce_category_image() {
        if ( is_product_category() ){
    	    global $wp_query;
    	    $cat = $wp_query->get_queried_object();
    	    $thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
    	    $image = wp_get_attachment_url( $thumbnail_id );
    	    if ( $image ) {
    	    	$meta = get_post($thumbnail_id);
    		    $caption = !empty( $meta->post_excerpt ) ? $meta->post_excerpt : '';
    		    echo '<figure class="wp-caption alignleft" id="attachment_'.$thumbnail_id.'"><img alt="'.$caption.'" src="'.$image.'" class="wp-image-'.$thumbnail_id.' size-full"><figcaption class="wp-caption-text">'.$caption.'</figcaption></figure>';
    		}
    	}
    }

    Chris

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Adding Caption to Product Category Thumb’ is closed to new replies.