WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] add image gallery in page template. (2 posts)

  1. artflutter
    Member
    Posted 5 years ago #

    Hi, I have a page called "Front Page" with a page id of 22. I added a single image to it and in the media library it has an id of 30.

    in my page template I want to insert this image and can do so with the code

    <?php echo wp_get_attachment_image( '30', 'full' ); ?>

    My question is, how do I, from within the loop get the ID of the gallery image associated with the post?

    Regards, Alex.

  2. artflutter
    Member
    Posted 5 years ago #

    Figured it out in the end. For reference code used is below.

    <?php
    $attargs = array(
    'post_type' => 'attachment',
    'post_mime_type' => 'image',
    'post_parent' => $post->ID);
    
    $attachments = get_children($attargs);
    if ($attachments) {
    	foreach ($attachments as $post) {
    		?>
    		<img src="<?php echo wp_get_attachment_url($post->ID); ?>" alt="<?php the_excerpt(); ?>" title="<?php the_title(); ?>" class="staff-img" />
    		<?php
    		break; //only display first image
    	}
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic