Post thumbnail outside the loop
try: global $post; if (has_post_thumbnail( $post->ID ) ){ // get_the_post_thumbnail($post->ID); // }
try: global $post; if (has_post_thumbnail( $post->ID ) ){ // get_the_post_thumbnail($post->ID); // }
Firstly, reverse the order in your conditional. You should use if ( false == $stop ) { … It’s called a “Yoda conditional” and is on the list of best practices for WP code. Secondly, media_sideload_image won’t return a WP error object if things fail. If things fail, it will return the ID of the … Read more
Unsetting sizes can normally be done easily via intermediate_image_sizes_advanced hook. The only problem is, there is only the $sizes array available, so this sufficient for a general level, but not if you want to do this conditionally. The information needed to do it conditionally can be gathered with debug_backtrace – debug_backtrace is performance-wise somewhat expensive … Read more
You are misreading the Codex. wp_get_attachment_image_src() works just fine with custom image sizes. Proof of concept: // copied from the Codex // https://codex.wordpress.org/Function_Reference/add_image_size if ( function_exists( ‘add_image_size’ ) ) { add_image_size( ‘category-thumb’, 300, 9999 ); //300 pixels wide (and unlimited height) add_image_size( ‘homepage-thumb’, 220, 180, true ); //(cropped) } Add an image to the Library, … Read more
Use the oembed_dataparse filter to modify the resulting HTML output by any given oembed call. Example: add_filter(‘oembed_dataparse’,’test’,10,3); function test($return, $data, $url) { if ($data->provider_name == ‘YouTube’) { return “<img src=”https://wordpress.stackexchange.com/questions/19500/{$data->thumbnail_url}”>”; } else return $return; } Then putting this in a post: Will give you a picture of Rick Astley instead of a flash video of … Read more
I needed just this for a recent project, so here’s my plugin! The code should be self-explanatory, but if there are any questions ask away. <?php /** * Plugin Name: oEmbed Featured Image * Plugin URI: http://wordpress.stackexchange.com/q/70752/1685 * Description: Automatically set the featured image if an oEmbed-compatible embed is found in the post content. * … Read more
Can’t recommend Get the Image plugin (by Justin Tadlock) enough. It’s among the best out there, very well maintained and neat code — more importantly, it does what you need, while giving you a whole slew of options. How does it pull images? Looks for an image by custom field (one of your choosing). If … Read more
Here is an example of adding a new component to the Featured Image area. This was taken from the official docs and updated to use JSX. I am using the @wordpress/scripts package to build this out. // Import from the hooks package import { addFilter } from ‘@wordpress/hooks’; // Some custom component to add. const … Read more
If you want to apply lazyload to each attachment image, you can just add your hoot to wp_get_attachment_image_attributes filter: add_filter( ‘wp_get_attachment_image_attributes”https://wordpress.stackexchange.com/questions/54986/,”wpse8170_add_lazyload_to_attachment_image’, 10, 2 ); function wpse8170_add_lazyload_to_attachment_image( $attr, $attachment ) { $attr[‘data-original’] = $attr[‘src’]; $attr[‘src’] = “https://wordpress.stackexchange.com/questions/54986/grey.gif”; return $attr; } Or if you can use second approach: $thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), “size” ); // … Read more
Have did some customization. This will help you show parent and child category images. You can later customize this code as per your requirements. $taxonomyName = “product_cat”; //This gets top layer terms only. This is done by setting parent to 0. $parent_terms = get_terms($taxonomyName, array(‘parent’ => 0, ‘orderby’ => ‘slug’, ‘hide_empty’ => false)); echo ‘<ul>’; … Read more