WordPress.org

Support

Support » Plugins and Hacks » [Resolved] Using only the first attached image

[Resolved] Using only the first attached image

  • Hello! First of all I would like to thank Mike for this really great Plugin! I am testing WP Tiles for the relaunch of my site and it works fantastic. The only thing I am missing is to force WP Tiles to use only the first attached image and no other image. I am sure this could be done, if you rearrange the following code from the end of wp-tiles.php. I have tried different changes, but due to a lack of PHP knowledge I seem uable to do it. Maybe someone could help me with this or point me in the right direction?

    public function get_first_image ( $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];
                }
    
                $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];
                }
    
                if ( ! empty ( $post->post_content ) ) {
                    $xpath = new DOMXPath( @DOMDocument::loadHTML( $post->post_content ) );
                    $src = $xpath->evaluate( "string(//img/@src)" );
                    return $src;
                }
    
                return '';
            }
    
        }

    http://wordpress.org/extend/plugins/wp-tiles/

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Mike Martel
    Participant

    @mike_cowobo

    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

    justwanttoaskaquestion
    Member

    @justwanttoaskaquestion

    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!

    Plugin Author Mike Martel
    Participant

    @mike_cowobo

    @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 '';
    }
    Plugin Author Mike Martel
    Participant

    @mike_cowobo

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

    Plugin Author Mike Martel
    Participant

    @mike_cowobo

    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 '';
    }
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘[Resolved] Using only the first attached image’ is closed to new replies.