WordPress.org

Support

Support » How-To and Troubleshooting » List child pages AND attachments

List child pages AND attachments

  • I’m struggling to find out how to do the following. Any help you be great.

    I have a site with a number of pages (not posts) that have either 1 or 2 attachments. I would like to list each page that is a child (eg of Page ID7) AND it’s attachments to create a simple ‘document download page’

    Parent (id7)
    — Child 1 | Download PDF attachment 1 | Download PDF attachment 2
    — Child 2 | Download PDF attachment 1 | Download PDF attachment 2
    etc…

    Cheers

Viewing 2 replies - 1 through 2 (of 2 total)
  • esmi
    Forum Moderator

    @esmi

    Try something like:

    <?php
    $args = array(
    	'post_type' => array( 'page' ),
    	'post_parent' => 7
    );?>
    <ul class="download-list">
    <?php $the_query = new WP_Query( $args );
    while ( $the_query->have_posts() ) : $the_query->the_post();?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title();?></a>
    <?php $attach_args = array(
    	'post_type' => array( 'attachment' ),
    	'post_parent' => $post-> ID
    );
    $my_attachments = = get_posts( $attach_args );
    if( $my_attachments ) :
    foreach($lastposts as $post) :
    setup_postdata($post);?>
    <ul class="downloads">
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    </ul>
    <?php endif;?>
    </li>
    <?php endwhile;?>
    </ul>

    WARNING: UNTESTED

    Unfortunately that didn’t work, even after removing the extra =

    $my_attachments = = get_posts( $attach_args );

    The nearest bit of code that I’ve found that does anything like is

    <?php
    
    $mypages = get_pages('child_of=7');
    foreach ( $mypages as $mypage  ) {
    $attachments = get_children(array(
    'post_parent' => $mypage->ID,
    'numberposts' => -1,
    'post_type' => 'attachment',
    'post_mime_type' => 'application/pdf', 
    
    ));?>
    
    <?php
    if ($attachments) {
    
        foreach ( $attachments as $post ) {
            setup_postdata($post);
            the_title();
    	the_attachment_link($post->ID, false);
    
        }
    }
    }
    ?>

    But it isn’t a bulleted list and it seems to only be showing 5 links from 5 random pages. Can’t work out why it’s showing those pages or why it’s only showing one of the two attachments per page.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘List child pages AND attachments’ is closed to new replies.