Custom Thumbnails not working at all - php

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.

Related

How to resize image using wordpress function

I want to resize image. Some times when we upload small image want to resize.
How can I do it?
Eg - When we upload 250px * 190px image I want to resize 450px * 200px
I used below code but it's not working:
add_image_size( 'img-fluid featured-purple-img', 475, 290, true ); // width, height, crop
Adding the Image Size is just the first step.
To make it avaible in the Media Uploader use this code in your functions.php
add_filter( 'image_size_names_choose', 'my_custom_sizes' );
function my_custom_sizes( $sizes ) {
return array_merge( $sizes, array(
'img-fluid featured-purple-img' => __( 'New Custom Size Name' ),
) );
}
If you want to use the image size in a template, this will do the trick:
if ( has_post_thumbnail() ) {
the_post_thumbnail( 'img-fluid featured-purple-img' );
}
Also I'm not quite sure how a whitespace in the image name affects functionality

Add Image Size in Wordpress not working

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...

How to Lower the Quality of Wordpress Featured Image (PHP)

All the posts I find are about people complaining that the image quality is too low however my images are too high quality and I would like to lower them to increase page speed. How could I set the resolution to 300 width by auto height in my functions.php file? Here is the code I am using to call it in my functions.php:
(Note): I do not want to hard crop or soft crop, I just want to affect resolution.
PHP:
<?php
function ericshio_theme_setup() {
add_theme_support( 'menus' );
add_theme_support( 'custom-logo' );
add_theme_support( 'custom-header' );
add_theme_support( 'custom-background' );
/* Post Thumbnails */
add_theme_support( 'post-thumbnails' );
add_theme_support( 'post-formats', array( 'aside', 'image', 'video' ));
register_nav_menu('Primary','Main Navigation');
}
add_action('init', 'ericshio_theme_setup');
?>

how can i stop these 2 wordpress functions from interfering with each other?

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' );

Wordpress post thumbnail 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' ); ?>

Categories