WordPress.org

Ready to get started?Download WordPress

Forums

WP Tiles
[resolved] Using attached instead of featured image (2 posts)

  1. fireaaway
    Member
    Posted 6 months ago #

    Hi!
    I want to force the plugin to use only the attached image (the first one), instead of the featured image.

    I did find this thread http://wordpress.org/support/topic/using-only-the-first-attached-image?replies=7, but I can't seem to get it to work. Some help would be greatly appreciated!

    In my wp-tiles-class.php, I'm not sure where to make the changes. I've found "Returns the first image" and "Finds the first relevant image to a post", and I don't really know which one is which... :)

    This is what I've tried - does not work (featured images are still used)

    /**
                 * Finds the first relevant image to a post
                 *
                 * Searches for a featured image, then the first attached image, then the first image in the source.
                 *
                 * @param WP_Post $post
                 * @return string Source
                 * @sice 0.5.2
                 */
                private function _find_the_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];
                    }
    
                    if ( !empty( $post->post_content ) ) {
                        $xpath = new DOMXPath( @DOMDocument::loadHTML( $post->post_content ) );
                        $src   = $xpath->evaluate( "string(//img/@src)" );
                        return $src;
                    }
                    return '';
                }

    https://wordpress.org/plugins/wp-tiles/

  2. Mike
    Member
    Plugin Author

    Posted 6 months ago #

    Hi there!

    You don't have to edit the plugin to achieve this. So make sure to reinstall the clean plugin files first.

    Next use the pre_wp_tiles_image filter to determine the used image. A function that will only return the first attached image would look something 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 '';
    }

    (to be sure, drop this in your functions plugin or functions.php file!)

    This is the same code as in the thread you referenced, I made a mistake there that I just corrected in an extra post!

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.