Ready to get started?Download WordPress


Displaying documents attached to parent page (2 posts)

  1. Elliot Toman
    Posted 2 years ago #

    I'm pretty clueless with PHP, but I put together a wp_query for a client's site that displays all the PDF files attached to the current page--and its parent. It works perfectly, except for a weird glitch: A handful of documents also appear on pages that have no attachments and no parent. Can anyone more proficient in the code than I am discern what might be causing the trouble?

    // Get the slug of the parent page.
    if($post->post_parent) { $post_data = get_post($post->post_parent);
    $post_parent_slug =  $post_data->post_name; }
    <?php // Documents attached to the parent page: 
    	// The Query
        $the_parents_query = new WP_Query( array( 'post_type' => 'page', 'name' => $post_parent_slug ) );
    	// The Loop
        while ( $the_parents_query->have_posts() ) : $the_parents_query->the_post(); 
    	$parentargs = array(
       'post_type' => 'attachment',
       'post_mime_type' => 'application/pdf,application/msword', // Only show PDFs or DOCs.
       'numberposts' => -1,
       'post_status' => null,
       'post_parent' => $post->ID
      $attachments = get_posts( $parentargs );
         if ( $attachments ) {
            foreach ( $attachments as $attachment ) {
               echo '<li>Download: <a title="Download Document" href="';
    		   echo wp_get_attachment_url( $attachment->ID );
    		   echo '">';
    		   echo apply_filters( 'the_title', $attachment->post_title );
    		   echo '</a></li>';
     endwhile; ?>
  2. Andrew
    Forum moderator
    Posted 2 years ago #

    Sorry, my mind has died.

Topic Closed

This topic has been closed to new replies.

About this Topic