WordPress.org

Ready to get started?Download WordPress

Forums

List child pages AND attachments (3 posts)

  1. jondobrien
    Member
    Posted 1 year ago #

    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

  2. esmi
    Forum Moderator
    Posted 1 year ago #

    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

  3. jondobrien
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic