Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've been having difficulties with some new functionality I've been trying to create. Basically I have a custom page setup that displays a list of all posts with a specific tag. What it needs to do is also display each attachment associated with that post, beside the post name. I can get this working on the actual post page, but since this is a custom page it doesn't want to display the attachment url.

Here is what I have so far:

$args = array 
    (
        'post_type' => 'attachment',
        'post_mime_type' => 'audio',
        'numberposts' => -1,
    );

    query_posts('portfolio-tags=apple&post_type=portfolio&posts_per_page=-1&orderby=title&order=asc'); // query to show all posts independant from what is in the center;
    if (have_posts()) :
        echo '<ul>';
        while (have_posts()) : the_post(); ?>
            <li>
                <span><?php echo the_title();?></span>
                <span><a href="<?php echo get_attachment_url($attachment->ID);?>" target="_blank">Demo</a></td>
            </li>
        <?php  endwhile;
        echo '</ul>';
    endif;
    wp_reset_query();

Any help would be greatly appreciated. I've been stuck on this for a while now and hope I'm just overlooking a very simple solution.

share|improve this question

2 Answers 2

First of all don't use query_posts function, use the WP_Query class instead.

Secondary you forget to fetch attachments for a post. You can get attachments list by calling get_children function.

$args = array (
    'post_type' => 'attachment',
    'post_mime_type' => 'audio',
    'numberposts' => -1,
);

$the_query = new WP_Query( 'portfolio-tags=apple&post_type=portfolio&posts_per_page=-1&orderby=title&order=asc' );
if ( $the_query->have_posts() ) :
    echo '<ul>';
    while ( $the_query->have_posts() ) : 
        $the_query->the_post();
        $images =& get_children( 'post_type=attachment&post_mime_type=image' ); ?>
        <li>
            <span><?php echo the_title();?></span>
            <?php foreach ( $images as $attachment_id => $attachment ) : ?>
                <span><a href="<?php echo get_attachment_url( $attachment_id );?>" target="_blank">Demo</a></td>
            <?php endforeach; ?>
        </li>
    <?php  endwhile;
    echo '</ul>';
endif;
wp_reset_postdata();
share|improve this answer
1  
Thanks for the prompt response! This should be displaying audio files, not images; regardless when I try it out and change the post_mime_type the "get_attachment_url" just shows up empty. Remember that this is not displaying on a normal post page, this is being displayed on a custom page I setup (wouldn't that mean that get_children won't work? Since that assumes the parent page is page.php) –  user1370773 Jun 14 '12 at 15:03
    
@William just remove &post_mime_type=image from get_children function and it should work –  Eugene Manuilov Jun 14 '12 at 15:13
    
Still not working, now instead of displaying all post items, it only displays one. The attachment url is still not displaying as an href. I should also mention that each attachment is categorized. I tried adding the category to the array, but no luck either. –  user1370773 Jun 14 '12 at 15:17
    
@William Please stop posting comments into the answer field. I have to clean this up manually. :/ Thanks. –  toscho Jun 14 '12 at 15:26

I have fixed this. I was forgetting to add the following in the get_children parameters: post_parent=$post->ID

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.