Hi Herzbert, thanks for your comment. You are at the right place for a solution. Change the code to this:
public function get_first_image ( $post ) {
$tile_image_size = apply_filters( 'wp-tiles-image-size', 'post-thumbnail', $post );
$images = get_children ( array (
'post_parent' => $post->ID,
'numberposts' => 1,
'post_mime_type' =>'image'
) );
if( ! empty ( $images ) ) {
$images = current ( $images );
$src = wp_get_attachment_image_src ( $images->ID, $size = $tile_image_size );
return $src[0];
}
return '';
}
}
I basically removed the check for post thumbnail and images in the source of the post. I’ll consider turning this into a filter in the next version, so you don’t have to hack the plugin files and you can keep updating it!
Mike
Hi Mike, thank you very much for your fast support! Your solution works perfect.
Best regards, Herzbert
Okay – I want to do the same thing – but I have no idea where to cut and paste this code in wp-tiles.php
I would like to get only the featured image, and instead it’s getting the first image. I have zero php skills so any help here would be much appreciated. By the way, beautiful plugin!
@justwanttoaskaquestion: In version 0.5.2 (coming today) this function is filterable by themes and plugins, so you don’t have to hack the plugin itself. Changing how the first image is loaded now works like this:
add_filter( 'pre_wp_tiles_image', 'my_tiles_first_image_function', 10, 2 );
function my_tiles_first_image_function( $src, $post ) {
// Your own code that handles which image is returned
}
So to force that it only takes the featured image, use use the filter like this:
add_filter( 'pre_wp_tiles_image', 'my_tiles_first_image_function', 10, 2 );
function my_tiles_first_image_function( $src, $post ) {
$tile_image_size = apply_filters( 'wp-tiles-image-size', 'post-thumbnail', $post );
$images = get_children( array(
'post_parent' => $post->ID,
'numberposts' => 1,
'post_mime_type' =>'image'
) );
if( !empty( $images ) ) {
$images = current( $images );
$src = wp_get_attachment_image_src( $images->ID, $size = $tile_image_size );
return $src[0];
}
return '';
}
@lindaeve: See the post above. However, the plugin will always use the featured image first, if it’s available, and only then look for an image inside the post.
To use the code above (the last block), add it at the bottom of the file functions.php in your theme folder. Make sure you replace ‘my_’ by something unique (your theme slug, for example).
For people coming in from Google: revisiting this thread, I found an important typo. The code sample above forces the use of only the first attached image.
To force only featured image do something like this instead:
add_filter( 'pre_wp_tiles_image', 'my_tiles_first_image_function', 10, 2 );
function my_tiles_first_image_function( $src, $post ) {
$tile_image_size = apply_filters( 'wp-tiles-image-size', 'post-thumbnail', $post );
if ( $post_thumbnail_id = get_post_thumbnail_id( $post->ID ) ) {
$image = wp_get_attachment_image_src( $post_thumbnail_id, $tile_image_size, false );
return $image[0];
}
return '';
}