Gallery post images on homepage?

I have a post with image gallery, it has 10 images. Can I get the direct link to the images in the post, so that I can display first 5 images on the homepage?

Related posts

Leave a Reply

3 comments

  1. You could use this loop for fetching the images from current post.

    <?php
    $images = get_posts(array(
        'post_parent' => get_the_ID(),
        'post_type' => 'attachment',
        'posts_per_page' => 5,
        'post_mime_type' => 'image'
    ));
    
    if( $image ) {
        foreach($images as $image) :
            echo wp_get_attachment_link( $image->ID, 'thumbnail-size', true );
        endforeach;
    }
    ?>
    

    reference:

    1. wp_get_attachment_link
    2. wp_get_attachment_image
  2. Add this to your child themes functions file and change the image I.D’s to match the ones you want included in the gallery.

    add_action( 'wp_head', 'wpsites_front_page_gallery' );
    function wpsites_front_page_gallery() {
    if ( is_home() ) {
    echo do_shortcode('[gallery columns="5" link="file" ids="45266,45258,45250,45229,45227" orderby="rand"]');
    }}
    

    enter image description here

    Change the wp_head hook to any WordPress or theme specific hook for positioning.