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".
Related
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.
I want to generate an extra image size in WordPress. WP lets you use add_image_size like this:
// Make sure featured images are enabled
add_theme_support( 'post-thumbnails' );
// Add featured image sizes
add_image_size( 'original-img', 2000, 99999 );
If the image has the min. width of 2000px, the image size will be generated.
However if the image is smaller the custom size won't be generated.
Is there any option to create an image for the custom image size everytime, no matter of the uploaded image size? Or maybe its possible to additionally save the original image?
To explain why I am trying to achieve: I need a watermark on my images, but need also the original image (or a very big image) as well without the watermark.
I also tried with the "large" image, but here the same problem. By uploading an image smaller than 1024px, the image size wouldn't be created.
You can hook into wp_generate_attachment_metadata and make a copy of the uploaded image there if it wasn't already created. I've included the code to do this below.
Add this to your functions.php. Each step is commented to explain what its doing, but basically during the upload process:
we check if WP created your custom size
If it was, do nothing
Otherwise create a copy of the image called imagename-WxH.extn (e.g.if the uploaded image was cat.jpg and was 700x520px, our copy will be called cat-700x520px.jpg)
Add the copied image as your custom size original-img
Then you can use your original-img custom size in your code and always have an image.
// Make sure featured images are enabled
add_theme_support( 'post-thumbnails' );
// Add featured image sizes
add_image_size( 'original-img', 2000, 99999 );
// Hook into the upload process
add_filter('wp_generate_attachment_metadata','copy_original_img');
// Check if the original image was added, if not make a copy and add it as original-img
function copy_original_img($image_data) {
// if the original-img was created, we don't need to do anything
if (isset($image_data['sizes']['original-img']))
return;
// 1. make a copy of the uploaded image to use for original-img
$upload_dir = wp_upload_dir();
$uploaded_img = $image_data['file'];
$img_w = $image_data['width'];
$img_h = $image_data['height'];
// construct the filename for the copy in the format: imagename-WxH.extn
$path_parts = pathinfo($uploaded_img);
$new_img = $path_parts['filename']."-".$img_w."x".$img_h.".".$path_parts['extension'];
// make a copy of the image
$img_to_copy = $upload_dir['path'] . '/'.$uploaded_img;
$new_img_path = $upload_dir['path'] . '/'.$new_img;
$success = copy($img_to_copy,$new_img_path);
// 2. If the image was copied successfully, add it into the image_data to be returned:
if ($success){
$image_data['sizes']['original-img']['file'] = $new_img;
$image_data['sizes']['original-img']['width'] = $img_w ; // same as uploaded width
$image_data['sizes']['original-img']['height'] = $img_h; // same as uploaded height
$image_data['sizes']['original-img']['mime-type'] = mime_content_type($new_img_path);
}
return $image_data;
}
I'm coding a shortcode that accept one media ID as an attribute. This shortcode should then display the image at an existing size registered with add_image_size. Problem : my image is not displayed at the correct size.
Explanations :
My media image has been uploaded in the WP library. The original file size is huge (1227x924). The media has the ID 294.
I registered an image size in functions.php :
function my_after_setup_theme(){
add_image_size('my-image-size', 210, 136, true);
}
add_action('after_setup_theme', 'my_after_setup_theme');
I insert my shortcode in one of my pages : [my_shortcode imageid="294"]
My shortcode code is :
function my_shortcode_func($atts)
{
$a = shortcode_atts(array(
'imageid' => 0,
), $atts);
if ($a['imageid'] == 0) {
// default placeholder image
$img = 'img src="http://placehold.it/210x136"/>';
} else {
// get resized (NOT WORKING !)
$img = wp_get_attachment_image($a['imageid'], 'my-image-size');
}
return $img;
}
add_shortcode('my_shortcode', 'my_shortcode_func');
I expect the original image to be resized at the correct size (ie : 210x136) in a new thumbnail file. Instead, the $img displays the original image (1227x924) , displayed at an intermediate size via the width and height attributes of the HTML <img> tag :
What am I doing wrong ?
thanks for help.
Did you register the thumbnail size after uploading the media? If so, you will need to use a plugin to regenerate the thumbnails.
Additionally, I don't believe you need to wrap thumbnail size declarations as you have done, I have the following;
if (function_exists('add_theme_support'))
{
add_theme_support('post-thumbnails'); // Featured image support for posts - you may not need this bit.
add_image_size('large', 700, '', true); // Large Thumbnail - This is the bit you would need
}
So it would be worth checking if your thumbnails are actually be generated to start with, as I have never done it the way you have defined in your question.
Im using the plugin "Dynamic_Featured_Image" for wordpress. I can get the image to show all the default image sizes (full/medium/thumb), but I want it to use a custom added one in my theme.
<?php
if( class_exists('Dynamic_Featured_Image') )
{
global $dynamic_featured_image;
$featured_images = $dynamic_featured_image->get_featured_images();
$i = 0;
foreach ($featured_images as $row=>$innerArray)
{
$id = $featured_images[$i]['attachment_id'];
$mediumSizedImage = $dynamic_featured_image->get_image_url($id,'conferance');
$caption = $dynamic_featured_image->get_image_caption( $mediumSizedImage );
echo " <li><img src=\"".$mediumSizedImage."\" alt=\"".$caption."\"><div class=\"captionText\">".$caption." </div></li>";
$i++;
}
}
?>
And the themesupported thumb is:
add_theme_support( 'post-thumbnails' );
add_image_size( 'conferance', 570, 335, true );
But when I fetch the image it still comes in the full (original size). Any clues why?
Your code is working as expected. Looks like you added the new image size conferance after uploading the images. WordPress doesn't regenerate the newly registered size, it only applies it to the future uploads. Please refer this thread.
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.