Attachment URL not working
I’ve been working on a redesign for a website on a local development setup and have decided to give a shot at WordPress for the first time. On my front page, I have an image slider that slides through the three most recently posted items that I want on the front page. On all of the posts, I have attached an image that I want to display on the front page. Here’s my code on the homepage:
<?php query_posts('category_name="main page"&showposts=3'); while ( have_posts() ) : the_post(); ?> <div class="panelContent"> <img class="panelPhoto" src="<?php echo wp_get_attachment_url(get_the_ID()); ?> "/> <div class="panelCaption"> <h2><?php the_title(); ?></h2> <?php the_excerpt(); ?> </div> </div> <?php endwhile; ?>
The code returns an underscore in the src part of the img tag. Not exactly sure why.
Also, here’s an image of my media panel showing that the images are indeed attached:
Ok, well I learned that the attachment functions are looking for the attachment ID and not the ID of the post they’re attached too. I’ve tried this code:
<?php query_posts('category_name="main page"&showposts=3'); while ( have_posts() ) : the_post(); ?> <div class="panelContent"> <?php $images = get_children(array( 'post_parent' => get_the_ID(), 'post_type' => 'attachment', 'numberposts' => 1, 'post_mime_type' => 'image',)); ?> <img class="panelPhoto" src="<?php echo wp_get_attachment_url($images->ID); ?> "/>
But I’m still having the same problem. The src attribute of the image tag still comes up blank. Any ideas?
I have the same problem. I’m also using
get_childrenfunction. I’ve attached several images to a page, but
get_childrenfunction returns an empty array.
I’ve checked the
Make WordPress-level attachment relationshipscheckbox in the plugin settings. Now, in the Media Library it says that an image is attached to a page. However, I couldn’t see an attachment record in the DB. I guess that’s the reason why
get_childrendoesn’t return anything.
How do I get all the image attachments of a page/post??
I would really appreciate any help!
I’ve found this link: http://mondaybynoon.com/wordpress-attachments/
$attachments = attachments_get_attachments();to get all attachments of a post/page.
One thing that I’m a bit confused about is why attachments relationships don’t appear in the wp_posts table like WP naive ones do. Is that the reason for it? I thought that the
Make WordPress-level attachment relationshipssetting is specifically for that reason – to add attachment relationships to the wp_posts table.
Would appreciate if someone can explain 🙂
- The topic ‘Attachment URL not working’ is closed to new replies.