0

I need to generate a list of attachments uploaded to a specific page (id=628). I thought this code was going to work, but it doesn't generate anything at all:

<?php
get_header(); ?>

<div id="container">
    <div id="content" role="main">
        <?php
            $attachments =& get_children( array (
                'post_parent' => 628,
                'post_type' => 'attachment'
            ));

            if ( empty($attachments) ) {
                // no attachments here
            } else {
                foreach ( $attachments as $attachment_id => $attachment ) {
                    echo wp_get_attachment_link( $attachment_id );
                }
            }
        ?>
    </div><!-- #content -->
</div><!-- #container -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

Any idea how I can get it to work?

  • Remove the & from this line $attachments =& get_children(. This seems like depreciated syntax or even invalid syntax. Also do a var_dump() on $attachments and see what is returned – Pieter Goosen Dec 17 '14 at 10:45
  • Ok, I removed the & and did a var_dump($attachments). It returned an empty array (I think): array(0) { }. – Garland Briggs Dec 17 '14 at 10:50
  • Jip, you are getting an empty array. According to the docs, your usage is correct (although the syntax is really old and not in use anymore). There is not much you can do to debug this. I would however test this this afternoon. Seems strange that your code is not working. Are you sure your id is correct – Pieter Goosen Dec 17 '14 at 11:00
  • I tested with myurl/?p=628 and it redirects to the page, so I'm sure. The page has a lot of documents attached, mainly PDFs. – Garland Briggs Dec 17 '14 at 11:05
  • Are you sure the pdf's are actually saved as attachments. Have you checked the wp_posts table to make sure about this – Pieter Goosen Dec 17 '14 at 11:21
0

The problem was that the files wasn't attached to the post. To make it work I re-attached the files (Media Library > Attach). I also removed the & in $attachments =& get_children( array (.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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