I'm trying to add images sizes on my wordpress site. I searched alot and tried too many solutions but couldn't work for me can anyone help me on this, it will be appreciatable. Thanks in advance. I have added this code in my functions.php file
add_filter( 'image_size_names_choose', 'my_custom_sizes' );
function my_custom_sizes( $sizes ) {
return array_merge( $sizes, array(
'slider_image_desktop' => __( 'slider_image_desktop' ),
) );
}
add_action( 'after_setup_theme', 'wpse_setup_theme' );
function wpse_setup_theme() {
add_theme_support( 'post-thumbnails' );
add_image_size( 'slider_image_desktop2', 60, 60, true );
}
Thanks everyone i solved my problem. I changed my some lines of code to
add_action( 'after_setup_theme', 'wpse_setup_theme' );
function wpse_setup_theme() {
add_theme_support( 'post-thumbnails' );
if ( function_exists( 'add_image_size' ) ) {
add_image_size( 'slider_image_desktop2', 60, 60, true );
}
}
It made the thumbnail then for retrieving i didn't use function for get image thumb because i am using Advanced Custom Fields so in wp admin in my acf fields group i just checked my image uploader's return value as image array and i just got full array of the uploaded image with my defined size. there was no need to use wp_get_attachment_image_src or wp_get_attachment_image function to fetch the image..
Thanks everyone for your help may this answer will help someone who will have been the same problem...
Try This:
$attachment_id = get_field('field_name');
$size = "slider_image_desktop2"; // (thumbnail, medium, large, full or custom size)
$image = wp_get_attachment_image_src( $attachment_id, $size );
// url = $image[0];
// width = $image[1];
// height = $image[2];
?>
<img class="image-class" alt="" src="<?php echo $image[0]; ?>" />
<?php
just copy and paste on your theme function.php file
add_image_size( 'img-cropped', 198, 198, true );// you can change the size
Your selected self answer was an absolute life saver, and got me to the point of what I was trying to do - but I had to make one small addition to get the custom image size in the backend page editor "add media" options:
function nmg_custom_sizes( $sizes ) {
return array_merge( $sizes, array(
'social-icons' => __('Social Icon'),
) );
}
add_action( 'after_setup_theme', 'nmg_setup_theme' );
function nmg_setup_theme() {
add_theme_support( 'post-thumbnails' );
if ( function_exists( 'add_image_size' ) ) {
add_image_size('social-icons', 9999, 60, false);
}
add_filter( 'image_size_names_choose', 'nmg_custom_sizes' );
}
It's your function, but with the function to add the custom size to image_size_names_choose - but after theme setup (what was stopping it from working for me before).
Might be useful to someone...
Related
I want to change the og:image that Yoast SEO generates, to some custom height and width (eg 1200x630) because I'm uploading very large images. I've tried adding this code in functions.php but doesn't work. Please help
function add_share_image_sizes() {
add_image_size( 'facebook-share', 1200, 630, true );
}
add_action( 'after_setup_theme', 'add_share_image_sizes' );
function set_yoast_facebook_share_image_size() {
return 'facebook-share';
}
add_filter( 'wpseo_opengraph_image_size', 'set_yoast_facebook_share_image_size' );
You need to pass a parameter.
function add_share_image_sizes() {
add_image_size( 'facebook-share', 1200, 630, true );
}
add_action( 'after_setup_theme', 'add_share_image_sizes' );
function set_yoast_facebook_share_image_size($img_size) {
return 'facebook-share';
}
add_filter( 'wpseo_opengraph_image_size', 'set_yoast_facebook_share_image_size' );
With Woocommerce and my Storefront child theme, I am trying to prevent the fact that when clicking on a single-product image, it opens a new page with the full size image.
I want the image to be unclickable so nothing happens if the user clicks on the image in the product page.
In my child-theme functions.php, the following code prevents zooming and opening the image in a lightbox, but I want to fully deactivate the linking.
add_action( 'after_setup_theme', 'remove_hemen_theme_support', 100 );
function remove_hemen_theme_support() {
remove_theme_support( 'wc-product-gallery-zoom' );
remove_theme_support( 'wc-product-gallery-lightbox' );
}
How can I achieve this?
Add this filter, it should remove your hyperlink
function e12_remove_product_image_link( $html, $post_id ) {
return preg_replace( "!<(a|/a).*?>!", '', $html );
}
add_filter( 'woocommerce_single_product_image_thumbnail_html', 'e12_remove_product_image_link', 10, 2 );
you can overwrite the template woocommerce/single-product/product-thumbnails.php in your theme.
it runs the:
apply_filters(
'woocommerce_single_product_image_thumbnail_html',
sprintf(
'%s',
esc_url( $props['url'] ),
esc_attr( $image_class ),
esc_attr( $props['caption'] ),
wp_get_attachment_image( $attachment_id, apply_filters( 'single_product_small_thumbnail_size', 'shop_thumbnail' ), 0, $props )
),
$attachment_id,
$post->ID,
esc_attr( $image_class )
);
How I did it with the same theme.
I created a simple plugin (but you can use functions.php) with functions I need for override the theme and inside I have this code:
add_action( 'wp', 'ts_remove_zoom_lightbox_gallery_support', 99 );
function ts_remove_zoom_lightbox_gallery_support() {
remove_theme_support( 'wc-product-gallery-zoom' );
remove_theme_support( 'wc-product-gallery-lightbox' );
}
The difference between our function is that I am using "wp" instead "after_setup_theme".
Try it and let me know if is work.
i have a specific word press theme and a plugin for counting downloads.... however when i activate the plugin it makes all my "featured thumbnails" disappear for all the post types in my wordpress... in other words there is no "featured image box" when adding any kind of post AFTER i activate the plugin......
after looking through my theme code and the plugin code i believe i have found the problem but i am not sure hoe to fix it.....
the theme i am using has the following function
// Add Thumbnail support
if (function_exists('add_theme_support')) { // Adding thumbnail support to all post types.
add_theme_support( 'post-thumbnails', array( 'store', 'dlm_download', 'service', 'album', 'mixtape', 'merch', 'photos', 'video' , 'download' , 'homebanner', 'post' ) );
add_image_size('storeimage', 200, 200, true); //Store homepage Thumbnails
add_image_size('beat_page', 160, 160, true); // Single beat page
add_image_size('photo', 200, 200, true); // Thumbnail image
add_image_size('photowidget', 165, 165, true); // Thumbnail image
add_image_size('videowidget', 270, 151, true); // Thumbnail image
add_image_size('photobig', 570, 570, true); // Image for single page
add_image_size('videoimage', 273, 200, true); // Video Gallery Homepage Thumbnails
add_image_size('homebannerimage', 920, 350, true); //Homepage Banner Image
add_image_size('featuredimage', 580, 250, true); //Post Featured image Image 580x250
}
and then the plugin has the following function
public function compatibility() {
// Post thumbnail support
if ( ! current_theme_supports( 'post-thumbnails' ) ) {
add_theme_support( 'post-thumbnails' );
remove_post_type_support( 'post', 'thumbnail' );
remove_post_type_support( 'page', 'thumbnail' );
} else {
// Get current supported
$current_support = get_theme_support( 'post-thumbnails' );
// This can be a bool or array. If array we merge our post type in, if bool ignore because it's like a global theme setting.
if ( is_array( $current_support ) ) {
add_theme_support( 'post-thumbnails', array_merge( $current_support, array( 'dlm_download' ) ) );
}
add_post_type_support( 'download', 'thumbnail' );
}
}
i noticed they both use "add_theme_support" , "post-thumbnails" and a few other similar things...... not sure why the two functions are interfering with each other or how i could fix it...... i tried to add the "dlm_download" post type (this is the post type for the the plugin) to the theme functions array of post types and it didnt make any difference.....
i tried to totally remove the thumbnail function from the plugin and kept getting a "failed to call header : head already called" error....
any help would be much appreciated!
Try changing this line in your theme
add_theme_support( 'post-thumbnails', array( 'store', 'dlm_download', 'service', 'album', 'mixtape', 'merch', 'photos', 'video' , 'download' , 'homebanner', 'post' ) );
to
add_theme_support( 'post-thumbnails' );
For some reason creating custom thumbnails won't work with WordPress, here's my code, any help would be greatly appreciated
Functions.php
if ( function_exists( 'add_theme_support' ) ) {
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 150, 150 ); // default Post Thumbnail dimensions
}
if ( function_exists( 'add_image_size' ) ) {
add_image_size( 'home-news', 560, 200, true );
}
Page Template
<div id="homeNewsThumbnail">
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail( 'home-news' );
} ?>
</div>
Crisis averted, apparently the theme caches everything and processes the crop sizes on upload, so all I needed to do was re-upload it and it produced the new crop size.
I have this line: <?php the_post_thumbnail( array(140,180) ); ?> But it still doesn't display the right sizes, is there a way to achive the exact sizes that I want?
Try adding a custom image size first and using a reference to that definition:
http://codex.wordpress.org/Function_Reference/add_image_size
In functions.php
if ( function_exists( 'add_theme_support' ) ) {
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 150, 150 ); // default Post Thumbnail dimensions
}
Then you can use <?php the_post_thumbnail( 'post-thumbnails' ); ?>