WordPress.org

Ready to get started?Download WordPress

Forums

Attachments links/urls driving me insane!!!!!! (5 posts)

  1. generalsalt
    Member
    Posted 5 years ago #

    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!!!!

  2. generalsalt
    Member
    Posted 5 years ago #

    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);
    	}
    }
    
    ?>
  3. yoshi
    Member
    Posted 5 years ago #

    'numberposts' => -1,

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

    'numberposts' => 1,

  4. yoshi
    Member
    Posted 5 years ago #

    'numberposts' => -1,

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

    'numberposts' => 1,

  5. generalsalt
    Member
    Posted 5 years ago #

    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.

  6. yoshi
    Member
    Posted 5 years ago #

    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?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.