WordPress post page not showing all images in gallery

I am hoping someone can assist, as this is killing me. I’m working with someone elses code and having lots of trouble and I’m only new at wordpress.

What I want to acheive is for the post to display the first image from the gallery at the top of the page, then the second image below it (next to some text content), then under that I want it to display all the remaining images in the gallery. For some reason, it’s currently leaving the last two images in the gallery out, as though they are being intentionally excluded. Thanks so much for your help I appreciate it.

Read More

This is the current code:

<?php

get_header();

if (have_posts()) {
while (have_posts()) {
    the_post();
    $images = GetPostImages(); // Get Post Images
    $tags = GetPostTags(); // Get Post Tags String
    ?>                    
<div class="project">

<img src="<?php echo $images[1]->guid; ?>" class="large" width="1000" height="700" />

<div class="projectdesc">  
    <h1><?php the_title(); ?></h1>
        <p class="tags"><?php echo $tags; ?></p>

    <?php 
the_content(); ?>

</div>

<img src="<?php echo $images[2]->guid; ?>" class="right" width="600" height="400" />

 <?php
        // Additional Project Images
        for ($i = 3; $i < count($images); $i++) {
            ?>
            <img src="<?php echo $images[$i]->guid; ?>" class="large" width="1000"/>        
            <?php
        }
        ?> 

    </div>

    <?php
}
}

get_footer();
?>`

The code in the functions.php file is also below. I think this controls the way the post page processes images?

function GetPostImages() {
global $post;

// Get image attachments
$images = get_children('post_type=attachment&post_mime_type=image&post_parent='.$post->ID);

// Sort by menu_order
foreach ($images as $key=>$image) {
    $newImages[$image->menu_order] = $image;
}

// Return
return $newImages;

}

Related posts

Leave a Reply

1 comment

  1. use wp_get_attachment_image_src() to get the image source

    $attr = wp_get_attachment_image_src(int $id,string $size); //where $id is image post id
    
    $width  =  $attr[1];
    $height =  $attr[2];
    $src    =  $attr[0]; //the source
    
    echo sprintf('<img src="%s" alt="" height="%s" width="%s"/>',$src,$height,$width);
    

    witin the functions.php file or an include file within the functions.php include…

    function setup_images(){
    
    //Register Image dimensions
    add_image_size( 'large',1000,700,true); //'large' will be the string used in $size
    add_image_size( 'med',600,400,true);  //or 'med'
    }
    add_action('after_setup_theme','setup_images'); //hook that function up in the right place