• I’ve been messing around with this all day and still no luck!!

    I am trying to get the first picture of each post to show on the index.php page (there will be approx 4-5 pics per post).

    I think I am approaching this whole thing wrong because I am getting for funky results…like foreach loops that spit EVERYTHING in the media gallery to only showing the first post. The more I do this more lost I get!

    I really need some help here!!!!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter generalsalt

    (@generalsalt)

    for example….

    In the Codex, it says put this (code below) in the Loop..do they mean in the Loop on the index.php or single.php? Because in the index.php if I have 7 posts, the code below only spits out the jpegs of the first post only. Arrrgghhh!!

    <?php
    
    $args = array(
    	'post_type' => 'attachment',
    	'numberposts' => -1,
    	'post_status' => null,
    	'post_parent' => $post->ID
    	);
    $attachments = get_posts($args);
    if ($attachments) {
    	foreach ($attachments as $attachment) {
    		echo apply_filters('the_title', $attachment->post_title);
    		the_attachment_link($attachment->ID, false);
    	}
    }
    
    ?>

    'numberposts' => -1,

    You are requesting all the attachments the post has. Change this to

    'numberposts' => 1,

    'numberposts' => -1,

    You are requesting all the attachments the post has. Change this to

    'numberposts' => 1,

    Thread Starter generalsalt

    (@generalsalt)

    i tried the code with a 1 instead, within the Loop. It pulls the first picture of the Media Library in. That pulled picture (from the Media Lib) is not even in my post! When I use a -1 it pulls all the pictures from the gallery.

    What I was trying to explain is that it is not pulling from my post, it is pulling from the Media Library.

    I’ve tried to put the above code in the Loop, outside the Loop, in it’s own Functions file.

    Thanks though…still am struggling with this.

    Sounds like $post->ID isn’t set correctly.
    The code must be placed within the Loop.
    If you do echo $post->ID, does that show the ID of each post?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Attachments links/urls driving me insane!!!!!!’ is closed to new replies.