Get current page's thumbnail URL with active sizes - wordpress

I have two ways of retrieving the current thumbnail URLs implemented in my plugin
$post_thumbnail_id = get_post_thumbnail_id( $post_id );
$attachment = wp_get_attachment_image_src( $post_thumbnail_id );
echo($attachment[0])
Output : wp-content/uploads/2019/10/image-150x150.jpg
the_post_thumbnail_url()
Output : wp-content/uploads/2019/10/image.jpg
But I'm looking for the URL of featured image with the size that is currently used in the article/page.
As an example : wp-content/uploads/2019/10/image-1128x484.jpg
But I wish to be able to retrieve the URL for whichever size is currently being used to display the image on the page/post.

To get the featured image URL, you can use get_the_post_thumbnail_url and supply the specific size you want as an argument.
$img_url = get_the_post_thumbnail_url($post_id,'full');
Replace full with the size you are looking to add.

Related

Set default image for woocommerce products

In my project I have many products whose image is not available till now. So I want to show custom image for those products who don't have image.
Is there a way to set default image for products (in case if product don't have image).
I solved it by adding following code in functions.php file
add_action( 'init', 'custom_fix_thumbnail' );
function custom_fix_thumbnail() {
add_filter('woocommerce_placeholder_img_src', 'custom_woocommerce_placeholder_img_src');
function custom_woocommerce_placeholder_img_src( $src ) {
$upload_dir = wp_upload_dir();
$uploads = untrailingslashit( $upload_dir['baseurl'] );
$src = $uploads . '/2012/07/thumb1.jpg';
return $src;
}
}
NOTE : Do not type complete url of image. Instead of complete url, use only short url as shown in code.
Original Post
TLDR;
You need to upload an image via default Wordpress Media uploader, then copy its URL (or just ID) and paste it to "WooCommerce->Settings->Products->Placeholder image"
Found an answer here: https://www.iqcomputing.com/support/articles/changing-the-woocommerce-default-image/

How to Add Attribute Terms Image in WooCommerce?

In WooCommerce, I have created an attribute "Brand", and added some terms, like "Brand One", "Brand Two" etc..
I want to add an image for each term.
Right now there is no option to add image in attribute terms.
Please tell me how to add image in attribute terms.
An admin link is like this:
.../wp-admin/edit-tags.php?taxonomy=pa_brand&post_type=product'
WooCommerce stores product attributes outside of the usual taxonomy table, so you'll need to go with something more WC-specific. Try the Variation Swatches and Photos extension.
UPDATE: You can use the Taxonomy Images plugin but you have to make a minor alteration. By default the plugin only displays taxonomies that are set to display in the admin area (i.e. the show_ui value is set to true). WooCommerce hides the product attribute taxonomies by default, so the plugin will not display them in the settings screen. You can change this behaviour by commenting out/deleting lines 402-402 of taxonomy-images.php:
if ( ! isset( $taxonomy->show_ui ) || empty( $taxonomy->show_ui ) )
continue
Removing these lines will allow he plugin to display all taxonomies, regardless of whether they are hidden or not.
Props to #helgatheviking for suggesting that plugin in the first place
With the "Variation Swatches" plugin (see the answer by #Dre), everything works smoothly, and getting the image is as easy as this:
$swatch_term = new WC_Swatch_Term( 'swatches_id', $term_id, $taxonomy, false,
'swatches_image_size' );
$html = '<img src="' . $swatch_term->thumbnail_src . '" alt=""/>';
The "Taxonomy Images" has not been updated since WP 3.6.1...
Technically a WooCommerce "attribute" is just a WordPress Custom Taxonomy. Therefore I would try something like the Taxonomy Images plugin.
"Variation Swatches and Photos extension" plugin is premium and no body would like to purchase that to serve such a purpose as getting an icon image for a brand.
The "Taxonomy Images" has not been updated since WP 3.6.1...
What I came up with is Category and Taxonomy Image and that does the job.
Here's how you can get the image URL:
if (function_exists('get_wp_term_image'))
{
$meta_image = get_wp_term_image($term_id);
//It will give category/term image url
}
echo $meta_image; // category/term image url
I've fixed this issue. Actually i was not passing the right value in taxonomy. I was using variation swatches plugin so was not know which value to pass for taxonomy. below is the working code. I was trying to fetch "brands" attributes list with images.
$attribute_taxonomies = wc_get_attribute_taxonomies();
$taxonomy_terms = array();
if ($attribute_taxonomies) :
foreach ($attribute_taxonomies as $tax) :
if (taxonomy_exists(wc_attribute_taxonomy_name($tax->attribute_name))) :
if($tax->attribute_name=="brands"){
$taxonomy_terms[$tax->attribute_name] = get_terms(wc_attribute_taxonomy_name($tax->attribute_name), 'number=6&orderby=name&hide_empty=1');
}
endif;
endforeach;
endif;
foreach ($taxonomy_terms as $item) :
foreach($item as $child):
//print_r($child);
$thumbnail_id = get_woocommerce_term_meta( $child->term_id, 'product_pa_brands', true );
$textureImg = wp_get_attachment_image_src( $thumbnail_id );
//we are getting image in $textureImg[0]
}
endforeach;
endforeach;

what should write in parameter of wp_get_attachment_image_src() in wordpress

i m using wp_get_attachment_image_src()in my template file of wordpress. and i want image from this url.
http://localhost/....../wp-content/uploads/2014/02/AceBailBondsKS.png
then what to write in parameter of this function i have used following.:-
syntax:- <?php wp_get_attachment_image_src( $attachment_id, $size, $icon ); ?>
then in place of $attachment_id what id should i pass 2014 or 2?
i have used like this
wp_get_attachment_image_src(2014)
Thanks very much for any help anyone can give.
Go to your WordPress admin media library and locate the image file. Take note of the Media ID.
When you click on the image in the media library look at the URL. It should say &post=123 - The value here (123) is the media ID.
This is the ID you enter.
wp_get_attachment_image_src( 123 );

Force specific thumbnail size using wp_get_attachment_image

I've seen all kind of solutions to this issue but none of them resolved my simple problem.
As you can see on the website I'm working on the thumbnails to the right are not equal.
I need to set them to be 214x121px as the thumbnails.
I've tried using plugins such as Regenerate thumbnails (after adding new image size to functions.php) and Custom image sizes, with no success.
I think it's obvious but <?php echo wp_get_attachment_image( $attachment->ID, array(214, 121), false );?> since as I understood Wordpress will bring the closest, considering proportions, image to the given size.
My Wordpress version is 3.6.
Any ideas/assistance will be appreciated.
I generally use wp_get_attachment_image_src for doing this, and here's how I do it:
If you've added your custom image size like this:
add_image_size( 'blog_featured_image', 214, 121, false); //Featured Image for Blog
Then in your template, you could call the custom image size like this:
<?php $imageID = get_field('featured_resource_image');
$imageURL = wp_get_attachment_image_src( $imageID, 'blog_featured_image' ); ?>
<img src="<?php echo $imageURL[0]; ?>" />
'featured_resource_image' is a custom field I create using Advanced Custom Fields plugin, but the get_field for it just returns the ID of the image selected. So I assign the image's ID to $imageID. I then get the image's object using wp_get_attachment_image_src, where I used the custom image size of 'blog_featured_image'. This returns an array with all the image's data. The first record in the array is the URL of the image, so I echo out $imageURL[0] to return the URL.
There's probably other ways to do it, possibly much better ways, but this way has always worked for me.
EDIT:
From looking at the function you are referencing, this should work:
Define your custom image size as outlined above. Then use this:
<?php wp_get_attachment_image( $attachment_id, 'blog_featured_image' ); ?>
Just set $attachment_id to equal your image's ID however you are currently grabbing the ID.

Wordpress Responsie Theme feature image not showing fully

I hae created a website and i haev set a blog with featured images but the images dont show full it just shows half of it even if i change it in css it still shows half.
can anyone help me please
Where the image is shown in code:
// has_post_thumbnail() - boolean
get_the_post_thumbnail($id, $size, $attr ) // gets any thumbnail
the_post_thumbnail( $size, $attr ) // for use inside the loop
// demand a custom featured image size on upload for a custom post type.
add_image_size( 'review_type', 245, 245, true);
// call that size
the_post_thumbnail( $size, $attr ) // for use inside the loop
When uploaded in the media library you can choose the upload image size. However WP creates 3 images upon upload and fits one of them into your page. The function above can specify which image is used by "small", "medium", "large".

Resources