I want to display brand logos on all product pages.
I've uploaded the logos in the theme editor by going to products->attributes->brands...clicking on the sprocket allows one to upload a thumbnail image.
There are several plugins that can achieve the same thing, but there must be a way to retrieve the information of the brand thumbnail.
If you're using the WooCommerce Brands plugin, the following code supports obtaining a brand thumbnail URL:
global $post;
$brands = wp_get_post_terms( $post->ID, 'product_brand' );
if ( $brands )
$brand = $brands[0];
if ( ! empty( $brand ) ) {
$thumbnail = get_brand_thumbnail_url( $brand->term_id );
$url = get_term_link( $brand->slug, 'product_brand' );
echo '<img class="woocommerce-brand-image-single" src="'. $thumbnail . '"/>';
}
.. If you are looking up via a term ID, then the following code will also obtain the thumbnail URL:
$url = get_brand_thumbnail_url( $term->term_id, 'full' );
Related
I'm building a website with some articles accessible only for Members. All articles, accessible or not are shown on a page and i would like to add an icon on top of the featured image for people to understand which articles they can see for free or not.
Could you help ? I've already read the doc about featured image but my brain can't make the whole thing...
Here's what i already tried :
function article_reserve( $title ) {
if ( has_tag( 'abonne' ) ) {
$title = 'test ' . $title;
}
return $title;
}
add_filter( 'the_title', 'article_reserve' );
It change the title but i'm trying to change the featured image
Also tried that after comments :
function wpse_post_thumbnail_html( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
// Optionally add any logic here for determining what markup to output.
// $html will be an empty string if there is no post thumbnail.
if ( has_tag( 'abonne' ) ) {
$new_html = '<img src="https://placekitten.com/g/600/600" alt="kitten">';
}
return $new_html;
}
add_action( 'post_thumbnail_html', 'wpse_post_thumbnail_html', 10, 5 );
How can I get the URL of the first image that is uploaded to the product gallery?
I am not talking about the featured image.
I would appreciate any suggestions.
The following will give you the first image Url from the product gallery:
global $post, $product;
if( is_a($product, 'WC_Product'))
$product = wc_get_product($post->ID);
$product = wc_get_product(40);
// Get the array of the gallery attachement IDs
$attachment_ids = $product->get_gallery_image_ids();
if( sizeof($attachment_ids) > 0 ){
$first_attachment_id = reset($attachment_ids);
$link = wp_get_attachment_image_src( $first_attachment_id, 'full' )[0];
// Output
echo $link;
}
I'm trying to add an image from the product gallery to the Woocommerce checkout. I've managed it on the cart using the following code:
$product = new WC_product($product_id);
$attachment_ids = $product->get_gallery_attachment_ids();
foreach( $attachment_ids as $attachment_id )
{
// Display Image instead of URL
echo wp_get_attachment_image($attachment_id, 'full');
}
But this won't work in the checkout. I've currently got the main product image showing using this code:
$thumbnail = apply_filters( 'woocommerce_in_cart_product_thumbnail', $_product->get_image(), $values, $cart_item_key );
echo $thumbnail;
But I can't figure out how to adapt it to allow the gallery image through.
Any help would be appreciated!
Cheers,
Ash
I've added multiple featured images to my wordpress site using the multiple post thumbnail plugin. I'm trying to display them all underneath the content with their descriptions. I can do it for the main featured image no problem. I can display the rest of the featured images no problem, but whenever I try to add description by it's the page description not the image description.
This is how I added the main image and description.
<?php the_post_thumbnail( 'product-thumbnail' );
echo get_post(get_post_thumbnail_id())->post_content; ?>
The remaining images are added as such:
<?php MultiPostThumbnails::the_post_thumbnail(get_post_type(), 'secondary-
image', NULL, 'product-thumbnail');
?>
And so forth (third, fourth)..
Can somebody help with how to add the descriptions for the rest?
To display your post thumbnail with its caption, simply paste the following code inside the loop:
<?php the_post_thumbnail();
echo get_post(get_post_thumbnail_id())->post_excerpt; ?>
You can also display entire image description by adding this code inside the post loop:
<?php the_post_thumbnail();
echo get_post(get_post_thumbnail_id())->post_content; ?>
the above codes for single images if you use multiple images use this code below
<?php
$the_post_images = get_children( array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_mime_type'=> 'image'
) );
foreach ($the_post_images as $the_post_image) {
// SHOW FEATURED IMAGE TITLES
echo get_the_title( $the_post_image->ID );
//SHOW IMAGE DESCRIPTION OR CAPTIONS
echo apply_filters( 'get_the_excerpt', $the_post_image->post_excerpt );
}
?>
To get image, its caption in multiple post thumbnails,
`
echo $secondimgPath = MultiPostThumbnails::get_post_thumbnail_url( get_post_type(), 'second-featured-image', NULL);
echo $secondimgIdAttachment = abcd_get_attachment_id_by_url($secondimgPath);
$size = array( 854,395, 'bfi_thumb' => true, 'quality' => 100);
echo $secondLargeImage[0] = wp_get_attachment_image( $secondimgIdAttachment,$size );
echo get_post( $secondimgIdAttachment )->post_excerpt;
endif; ?>`
In functions.php, use the following -
function abcd_get_attachment_id_by_url( $url ) {
// Split the $url into two parts with the wp-content directory as the separator
$parsed_url = explode( parse_url( WP_CONTENT_URL, PHP_URL_PATH ), $url );
// Get the host of the current site and the host of the $url, ignoring www
$this_host = str_ireplace( 'www.', '', parse_url( home_url(), PHP_URL_HOST ) );
$file_host = str_ireplace( 'www.', '', parse_url( $url, PHP_URL_HOST ) );
// Return nothing if there aren't any $url parts or if the current host and $url host do not match
if ( ! isset( $parsed_url[1] ) || empty( $parsed_url[1] ) || ( $this_host != $file_host ) ) {
return;
}
// Now we're going to quickly search the DB for any attachment GUID with a partial path match
// Example: /uploads/2013/05/test-image.jpg
global $wpdb;
$attachment = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->prefix}posts WHERE guid RLIKE %s;", $parsed_url[1] ) );
// Returns null if no attachment is found
return $attachment[0];
}
I'm working on a gallery for the homepage of this site: Warm Glow Photo
I have called the featured image at a specific size (800x600) that I have defined in functions.php. This seems to be calling
...IMAGE.jpg?resize=800%2C600
instead of the version of the featured image called
...IMAGE-800x600.jpg
I am using a plug-in for cropping thumbnails which means I need to call this image rather than cropping with ?resize.
I've found lots of info about how to call different size thumbnails but nothing that explains why it crops with ?resize instead of calling the different thumbnail itself. Any ideas on how to do this would be much appreciated.
The relevant code is:
<?php
if ( has_post_thumbnail() ) {
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' );
echo '<li><a href="' . $large_image_url[0] . '" title="' . the_title_attribute( 'echo=0' ) . '">';
the_post_thumbnail( 'bones-thumb-800' );
echo '</a></li>';
}
?>
and defining bones-thumb-800 in functions.php
add_image_size( 'bones-thumb-800', 800, 600, true );
To get the plain URL of a post use this function :
function um_get_post_featured_image_src($post_id = null,$size = "full"){
if(!$post_id){
global $post;
$post_id = $post->ID;
}
if(has_post_thumbnail($post_id)){
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), $size );
return $image[0];
}else{
return "";
}
}
If you have a $post globally than you don't need to pass post ID.