wp_get_attachment_image example


        $custom_logo_attr = apply_filters( 'get_custom_logo_image_attributes', $custom_logo_attr$custom_logo_id$blog_id );

        /* * If the alt attribute is not empty, there's no need to explicitly pass it * because wp_get_attachment_image() already adds the alt attribute. */
        $image = wp_get_attachment_image( $custom_logo_id, 'full', false, $custom_logo_attr );

        if ( $unlink_homepage_logo && is_front_page() && ! is_paged() ) {
            // If on the home page, don't link the logo to home.             $html = sprintf(
                '<span class="custom-logo-link">%1$s</span>',
                $image
            );
        } else {
            $aria_current = is_front_page() && ! is_paged() ? ' aria-current="page"' : '';

            $html = sprintf(
                
if ( 'custom' === $size || ! in_array( $sizearray_merge( get_intermediate_image_sizes(), array( 'full' ) ), true ) ) {
                $size  = array( $instance['width']$instance['height'] );
                $width = $instance['width'];
            } else {
                $caption_size = _wp_get_image_size_from_meta( $instance['size']wp_get_attachment_metadata( $attachment->ID ) );
                $width        = empty( $caption_size[0] ) ? 0 : $caption_size[0];
            }

            $image_attributes['class'] .= sprintf( ' attachment-%1$s size-%1$s', is_array( $size ) ? implode( 'x', $size ) : $size );

            $image = wp_get_attachment_image( $attachment->ID, $size, false, $image_attributes );

        } else {
            if ( empty( $instance['url'] ) ) {
                return;
            }

            $instance['size'] = 'custom';
            $caption          = $instance['caption'];
            $width            = $instance['width'];
            $classes          = 'image ' . $instance['image_classes'];
            if ( 0 === $instance['width'] ) {
                

        do_action( 'begin_fetch_post_thumbnail_html', $post->ID, $post_thumbnail_id$size );

        if ( in_the_loop() ) {
            update_post_thumbnail_cache();
        }

        $html = wp_get_attachment_image( $post_thumbnail_id$size, false, $attr );

        /** * Fires after fetching the post thumbnail HTML. * * @since 2.9.0 * * @param int $post_id The post ID. * @param int $post_thumbnail_id The post thumbnail ID. * @param string|int[] $size Requested image size. Can be any registered image size name, or * an array of width and height values in pixels (in that order). */
        


    if ( empty( $attachments ) ) {
        return '';
    }

    if ( is_feed() ) {
        $output = "\n";
        foreach ( $attachments as $att_id => $attachment ) {
            if ( ! empty( $atts['link'] ) ) {
                if ( 'none' === $atts['link'] ) {
                    $output .= wp_get_attachment_image( $att_id$atts['size'], false, $attr );
                } else {
                    $output .= wp_get_attachment_link( $att_id$atts['size'], false );
                }
            } else {
                $output .= wp_get_attachment_link( $att_id$atts['size'], true );
            }
            $output .= "\n";
        }
        return $output;
    }

    


    $url = wp_get_attachment_url( $_post->ID );

    if ( $permalink ) {
        $url = get_attachment_link( $_post->ID );
    }

    if ( $text ) {
        $link_text = $text;
    } elseif ( $size && 'none' !== $size ) {
        $link_text = wp_get_attachment_image( $_post->ID, $size$icon$attr );
    } else {
        $link_text = '';
    }

    if ( '' === trim( $link_text ) ) {
        $link_text = $_post->post_title;
    }

    if ( '' === trim( $link_text ) ) {
        $link_text = esc_html( pathinfo( get_attached_file( $_post->ID ), PATHINFO_FILENAME ) );
    }

    
$attachment_id = $post->ID;

        if ( has_post_thumbnail( $post ) ) {
            $thumbnail_id = get_post_thumbnail_id( $post );

            if ( ! empty( $thumbnail_id ) ) {
                $attachment_id = $thumbnail_id;
            }
        }

        $title      = _draft_or_post_title();
        $thumb      = wp_get_attachment_image( $attachment_id, array( 60, 60 ), true, array( 'alt' => '' ) );
        $link_start = '';
        $link_end   = '';

        if ( current_user_can( 'edit_post', $post->ID ) && ! $this->is_trash ) {
            $link_start = sprintf(
                '<a href="%s" aria-label="%s">',
                get_edit_post_link( $post->ID ),
                /* translators: %s: Attachment title. */
                esc_attr( sprintf( __( '&#8220;%s&#8221; (Edit)' )$title ) )
            );
            $link_end = '</a>';
        }
<?php                 /** * Filter the default image attachment size. * * @since Twenty Twenty-One 1.0 * * @param string $image_size Image size. Default 'full'. */
                $image_size = apply_filters( 'twenty_twenty_one_attachment_size', 'full' );
                echo wp_get_attachment_image( get_the_ID()$image_size );
                ?> <?php if ( wp_get_attachment_caption() ) : ?> <figcaption class="wp-caption-text"><?php echo wp_kses_post( wp_get_attachment_caption() ); ?></figcaption> <?php endif; ?> </figure><!-- .wp-block-image --> <?php             the_content();

            wp_link_pages(
                

        $size = apply_filters( 'admin_post_thumbnail_size', $size$thumbnail_id$post );

        $thumbnail_html = wp_get_attachment_image( $thumbnail_id$size );

        if ( ! empty( $thumbnail_html ) ) {
            $content  = sprintf(
                $set_thumbnail_link,
                esc_url( $upload_iframe_src ),
                ' aria-describedby="set-post-thumbnail-desc"',
                $thumbnail_html
            );
            $content .= '<p class="hide-if-no-js howto" id="set-post-thumbnail-desc">' . __( 'Click the image to edit or update' ) . '</p>';
            $content .= '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail">' . esc_html( $post_type_object->labels->remove_featured_image ) . '</a></p>';
        }
    }
if ( current_user_can( 'edit_post', $post->ID ) ) {
            $post_link  = "<a href='" . get_edit_post_link( $post->ID ) . "' class='comments-edit-item-link'>";
            $post_link .= esc_html( get_the_title( $post->ID ) ) . '</a>';
        } else {
            $post_link = esc_html( get_the_title( $post->ID ) );
        }

        echo '<div class="response-links">';

        if ( 'attachment' === $post->post_type ) {
            $thumb = wp_get_attachment_image( $post->ID, array( 80, 60 ), true );
            if ( $thumb ) {
                echo $thumb;
            }
        }

        echo $post_link;

        $post_type_object = get_post_type_object( $post->post_type );
        echo "<a href='" . get_permalink( $post->ID ) . "' class='comments-view-item-link'>" . $post_type_object->labels->view_item . '</a>';

        echo '<span class="post-com-count-wrapper post-com-count-', $post->ID, '">';
        

function set_post_thumbnail( $post$thumbnail_id ) {
    $post         = get_post( $post );
    $thumbnail_id = absint( $thumbnail_id );
    if ( $post && $thumbnail_id && get_post( $thumbnail_id ) ) {
        if ( wp_get_attachment_image( $thumbnail_id, 'thumbnail' ) ) {
            return update_post_meta( $post->ID, '_thumbnail_id', $thumbnail_id );
        } else {
            return delete_post_meta( $post->ID, '_thumbnail_id' );
        }
    }
    return false;
}

/** * Removes the thumbnail (featured image) from the given post. * * @since 3.3.0 * * @param int|WP_Post $post Post ID or post object from which the thumbnail should be removed. * @return bool True on success, false on failure. */

            $shape = apply_filters( 'embed_thumbnail_image_shape', $shape$thumbnail_id );
        }

        if ( $thumbnail_id && 'rectangular' === $shape ) :
            ?> <div class="wp-embed-featured-image rectangular"> <a href="<?php the_permalink(); ?>" target="_top"> <?php echo wp_get_attachment_image( $thumbnail_id$image_size ); ?> </a> </div> <?php endif; ?> <p class="wp-embed-heading"> <a href="<?php the_permalink(); ?>" target="_top"> <?php the_title(); ?> </a> </p> <?php if ( $thumbnail_id && 'square' === $shape ) : ?>
Home | Imprint | This part of the site doesn't use cookies.