WordPress.org

Ready to get started?Download WordPress

Forums

Attachment URL not working (4 posts)

  1. jettj09
    Member
    Posted 3 years ago #

    Hey guys,

    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:

    http://imgur.com/D0FoO.png

  2. jettj09
    Member
    Posted 3 years ago #

    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[0]->ID); ?> "/>

    But I'm still having the same problem. The src attribute of the image tag still comes up blank. Any ideas?

  3. dashaluna
    Member
    Posted 2 years ago #

    I have the same problem. I'm also using get_children function. I've attached several images to a page, but get_children function returns an empty array.

    I've checked the Make WordPress-level attachment relationships checkbox 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_children doesn't return anything.

    How do I get all the image attachments of a page/post??

    I would really appreciate any help!

    Thanks,
    Dasha

  4. dashaluna
    Member
    Posted 2 years ago #

    I've found this link: http://mondaybynoon.com/wordpress-attachments/

    Use $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 relationships setting is specifically for that reason - to add attachment relationships to the wp_posts table.

    Would appreciate if someone can explain :)

    Thanks,
    Dasha

Topic Closed

This topic has been closed to new replies.

About this Topic