WordPress.org

Ready to get started?Download WordPress

Forums

WP Tiles
[resolved] Using only the first attached image (8 posts)

  1. herzbert
    Member
    Posted 1 year ago #

    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/

  2. Mike
    Member
    Plugin Author

    Posted 1 year ago #

    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

  3. herzbert
    Member
    Posted 1 year ago #

    Hi Mike, thank you very much for your fast support! Your solution works perfect.
    Best regards, Herzbert

  4. justwanttoaskaquestion
    Member
    Posted 1 year ago #

    Okay - I want to do the same thing - but I have no idea where to cut and paste this code in wp-tiles.php

  5. LindaEve
    Member
    Posted 1 year ago #

    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!

  6. Mike
    Member
    Plugin Author

    Posted 12 months ago #

    @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 '';
    }
  7. Mike
    Member
    Plugin Author

    Posted 12 months ago #

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

  8. Mike
    Member
    Plugin Author

    Posted 5 months ago #

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

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic