47

Yep - you can pass the class you want to use to the_post_thumbnail() as part of the attributes argument, for example <?php the_post_thumbnail('thumbnail', array('class' => 'your-class-name')); ?> Ref: http://codex.wordpress.org/Function_Reference/the_post_thumbnail#Styling_Post_Thumbnails


28

When an image is uploaded in WordPress, it is saved in its original size, and also as some resized copies in different sizes. In this way it becomes easy to use different sizes, of the same image, for different purposes. By default, WordPress creates 3 copies in 3 different sizes: 'thumb' 'medium' 'large' The size in pixels for these 3 sizes can be set in ...


27

By default all custom post adds support for Title and editor, if you want more stuff like comments, thumbnail and revisions you have to add it manually in the support argument. Read more about how to register your custom post type here, you can also find the section about support to see what you can add. Here is an example wehere to register thumbnail for ...


27

Can't this simply be done with media_sideload_image() ? Seems pretty simple. Only catch is if you aren't on admin area, you must include some libraries from within WordPress includes: // only need these if performing outside of admin environment require_once(ABSPATH . 'wp-admin/includes/media.php'); require_once(ABSPATH . 'wp-admin/includes/file.php'); ...


27

Your problem is that you are not providing the correct attachment's ID to get_post_meta() and get_the_title() functions. This is your code to get the alt of the image: $image_alt = get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true); And it is correct, but $attachment->ID is not defined in your code, so, the function does not return ...


26

I put the following code into my functions.php file. It works! add_filter( 'wp_image_editors', 'change_graphic_lib' ); function change_graphic_lib($array) { return array( 'WP_Image_Editor_GD', 'WP_Image_Editor_Imagick' ); } When this helps it is because it changes the PHP code module used for processing the uploaded image for use with WordPress. This ...


26

Came here as this post is among the top hits on the search engine when looking for WordPress image alt and title. Being rather surprised that none of the answers seem to provide a simple solution matching the question's title I'll drop what I came up with in the end hoping it helps future readers. // An attachment/image ID is all that's needed to retrieve ...


25

If you want display an image that is inserted into your content (a hotlinked image, for example), you must use a function like this (source): add in functions.php: function catch_that_image() { global $post, $posts; $first_img = ''; ob_start(); ob_end_clean(); $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->...


25

As of WordPress 3.5, the Meta Query Parameters support EXISTS and NOT EXISTS as compare operators. In the case where you are using these, you can omit the value part of the meta query. Try the following arguments for your query: $args = array( 'post_type' => 'post', 'posts_per_page' => 6, 'meta_query' => array( array( '...


22

After troubleshooting with @Wyck in chat, we have narrowed to the underlying issue. The issue was related to my server configuration not having the proper amount of memory allocated to Apache/PHP. If anyone has this same problem, please try verifying that you have enough (64 MB+) server memory allocated to Apache/PHP in your server configuration ...


22

Did you try anything? Its always helpful to share what you have tried. $url = wp_get_attachment_url( get_post_thumbnail_id($post_id) ); Or if you want to get the image by image size. $src = wp_get_attachment_image_src( get_post_thumbnail_id($post_id), 'thumbnail_size' ); $url = $src[0]; http://codex.wordpress.org/Function_Reference/get_post_thumbnail_id ...


19

post_excerpt is actually the caption attribute. Here is the correct answer: $title = get_post(get_post_thumbnail_id())->post_title; //The Title $caption = get_post(get_post_thumbnail_id())->post_excerpt; //The Caption $description = get_post(get_post_thumbnail_id())->post_content; // The Description


17

I figured it out! In my php.ini file I had to uncomment extension=php_gd2.dll, then it started working after I rebuilt the images I uploaded :) Thanks, Josh


15

Wordpress codex has the answer, its below. Set the image size by cropping the image and defining a crop position: add_image_size( 'custom-size', 220, 220, array( 'left', 'top' ) ); // Hard crop left top When setting a crop position, the first value in the array is the x axis crop position, the second is the y axis crop position. ...


14

if you are referring to the 'page for posts', then try (only relevant section of your code shown): if (is_home() && get_option('page_for_posts') ) { $img = wp_get_attachment_image_src(get_post_thumbnail_id(get_option('page_for_posts')),'full'); $featured_image = $img[0]; } else {


14

To built on Max Yudin's answer you should use the intermediate_image_sizes_advanced filter, and not image_size_names_choose. Add to functions.php function add_image_insert_override($sizes){ unset( $sizes['thumbnail']); unset( $sizes['medium']); unset( $sizes['large']); return $sizes; } add_filter('intermediate_image_sizes_advanced', '...


14

I found the culprit! WordPress 5.3 introduced additional image sizes which can be found via /wp-includes/media.php. Updating my function, like so, removed the extra sizes: function remove_default_image_sizes( $sizes) { unset( $sizes['large']); // Added to remove 1024 unset( $sizes['thumbnail']); unset( $sizes['medium']); unset( $sizes['...


13

For custom posts, you first have to enable support for thumbnails: add_theme_support( 'post-thumbnails' ); function theme_setup() { register_post_type( 'yourposttype', array( ..., 'supports' => array('title', ...,'thumbnail'), )); } add_action( 'after_setup_theme', 'theme_setup' );


13

You need to echo the return value from wp_get_attachment_image_src(). It also returns an Array(), so you need to grab the needed part from that array. In this case it's the first/0 value. Example: <?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );?> <div id="post" class="your-class" style=&...


13

I don't remember a function that will do precisely that, but it is easily achieved with API overall: Retrieve attachment ID with get_post_thumbnail_id() Retrieve available sizes with get_intermediate_image_sizes() For each size use wp_get_attachment_image_src(), which gives precisely data you need (URL and dimensions).


13

I want to disable the srcset only when calling a specific thumbnail size (for example only when calling full image size). Here are two ideas (if I understand you correctly): Approach #1 Let's check the size from the post_thumbnail_size filter. If it matches a corresponding size (e.g. full) then we make sure the $image_meta is empty, with the ...


12

I'd like to improve Robs answer by utilizing the WP core functions download_url and media_handle_sideload <?php /** * Downloads an image from the specified URL and attaches it to a post as a post thumbnail. * * @param string $file The URL of the image to download. * @param int $post_id The post ID the post thumbnail is to be associated with. * @...


12

I recently had to do this via a nightly cron script for a social media stream. $parent_id is the ID of the post you want to attach the image to. function uploadRemoteImageAndAttach($image_url, $parent_id){ $image = $image_url; $get = wp_remote_get( $image ); $type = wp_remote_retrieve_header( $get, 'content-type' ); if (!$type) ...


11

You can use the post_thumbnail_html filter to remove the attribute: function remove_img_attr ($html) { return preg_replace('/(width|height)="\d+"\s/', "", $html); } add_filter( 'post_thumbnail_html', 'remove_img_attr' ); Place this in your functions.php file


11

This is what I'm using, cobbled together from snippets found online... It's uses a filter on manage_posts_colummns to re-jig the headers and an action on manage_posts_custom_column to add the row level data. function custom_columns( $columns ) { $columns = array( 'cb' => '<input type="checkbox" />', 'featured_image' => 'Image'...


11

Ok, so I think I may have come to a solution. It doesn't seem as nice and easy as I'd like it to be, but then again major modifications to default Wordpress functionality sometimes requires drastic measures. :) This is my working solution to rewriting the HTML output for post thumbnails throughout my site to work with the Retinise.js plugin. Of course this ...


11

This example uses the alt text as it set in the media library. I prefer it because the behavior is more consistent for users who enter alt text using the typical WordPress options. $thumbnail_id = get_post_thumbnail_id( $post->ID ); $alt = get_post_meta($thumbnail_id, '_wp_attachment_image_alt', true); the_post_thumbnail( 'full', array( 'alt' => $...


10

You can use the wp_generate_attachment_metadata filter: function wpse_183835_to_jpeg( $meta, $post_id ) { $sizes_to_convert = array( 'thumbnail', ); $path = dirname( get_attached_file( $post_id ) ); foreach ( $sizes_to_convert as $size ) { if ( ! empty( $meta['sizes'][ $size ] ) ) { $data = $meta['sizes'][ $size ...


9

Gee, I keep solving my own problems all the time. Here's how I solved it in the end. I discovered that add_image_size does not ignore the identical dimensions of the image size, but instead points the file name towards the same file in the uploads dir. Once I knew that, I could just save the grayscale image under a different name, return that name to the $...


9

You could do it by adding an action to the hook 'rss2_item' like so: add_action('rss2_item', function(){ global $post; $output = ''; $thumbnail_ID = get_post_thumbnail_id( $post->ID ); $thumbnail = wp_get_attachment_image_src($thumbnail_ID, 'thumbnail'); $output .= '<post-thumbnail>'; $output .= '<url>'. $thumbnail[0] .'</...


Only top voted, non community-wiki answers of a minimum length are eligible