• Resolved jtm12

    (@jtm12)


    I am trying to list the attachment of a custom post displayed by parent and child taxonomies.

    The following code, which I appreciate someone else posting, works perfectly to display custom posts under a parent taxonomy (in this case a year) and then a child taxonomy.

    
    <?php
    // List posts by the terms for a custom taxonomy of any post type
    
    $post_type = 'company';
    $tax = 'client-topic';
    $tax_args = array(
    'order' => 'DESC',
    'parent' => 0
    );
    // get all the first level terms only
    $tax_terms = get_terms( $tax, $tax_args );
    if ($tax_terms) {
    foreach ($tax_terms as $tax_term) { // foreach first level term
    // print the parent heading
    ?>
    <h4 class="parent-term"><?php echo $tax_term->name; ?></h4>
    
    <?php
    // get all its children
    $child_terms = ""; // first ensure this var is empty
    $child_terms = get_terms ( $tax, array('order' => 'DESC', 'parent' => $tax_term->term_id) );
    // store an array of child terms slug
    $child_terms_array = array();
    foreach ($child_terms as $child_term){
    $child_terms_array[] = $child_term->slug;
    }
    
    // print the posts of the parent, but excluding the one which are also into a child term
    $parent_args="";
    $parent_args = array(
    'post_type' => $post_type,
    'tax_query' => array(
    'relation' => 'AND',
    array(
    'taxonomy' => $tax,
    'field' => 'slug',
    'terms' => $tax_term->slug,
    'include_children' => false,
    'operator' => 'IN'
    ),
    array(
    'taxonomy' => $tax,
    'field' => 'slug',
    'terms' => $child_terms_array,
    'include_children' => false,
    'operator' => 'NOT IN'
    )
    ),
    'post_status' => 'publish',
    'posts_per_page' => 6,
    'order' => 'ASC',
    );
    // query the posts
    $parent_query = null;
    $parent_query = new WP_Query($parent_args);
    
    if( $parent_query->have_posts() ) : ?>
    <ul>
    <?php while ( $parent_query->have_posts() ) : $parent_query->the_post(); ?>
    <li><a href="<?php the_permalink() ?>">
    <?php the_title(); ?>
    </a>
    </li>
    <?php endwhile; // end of loop ?>
    </ul>
    
    <?php endif; // if have_posts()
    wp_reset_query();
    
    // if any, foreach child term, query the posts
    if ( !empty($child_terms) ){
    foreach ($child_terms as $child_term){
    $child_args="";
    $child_args = array(
    'post_type' => $post_type,
    'tax_query' => array(
    array(
    'taxonomy' => $tax,
    'field' => 'slug',
    'terms' => $child_term->slug,
    'include_children' => false,
    'operator' => 'IN'
    )
    ),
    'post_status' => 'publish',
    'posts_per_page' => 6,
    'order' => 'ASC',
    );
    // query the posts
    $child_query = null;
    $child_query = new WP_Query($child_args);
    
    if( $child_query->have_posts() ) : ?>
    
    <h4 class="child-term"><?php echo $child_term->name; ?></h4>
    <ul>
    <?php while ( $child_query->have_posts() ) : $child_query->the_post(); ?>
    <li><a href="<?php the_permalink() ?>">
    <?php the_title(); ?>
    </a>
    </li>
    <?php endwhile; // end of loop ?>
    
    </ul>
    
    <?php endif; // if have_posts()
    
    wp_reset_query();
    } // end foreach #child_terms
    }
    
    } // end foreach #parent_term
    }
    ?>
    

    But the code displays post titles. I would like to instead get the attachment of each custom post and display the title and link. I have been able to do this in other scenarios by using this code directly below:

    
    <?php 
    $the_query->the_post();
    			$args = array(		
    			'order'          => 'DESC',
    			'post_type'      => 'attachment',
    			'post_parent'    => $post->ID,
    			'post_status'    => null,
    			'numberposts'    => 1,
    		);
    			$attachments = get_posts($args);
    			if ($attachments) {
    			foreach ($attachments as $attachment) { ?>
    
    			<a href="<?php echo wp_get_attachment_url( $attachment->ID, true ); ?>"><?php the_title(); ?></a><br />
    			<?php }
    } 
    ?>
    

    I am not skilled enough yet to combine the two pieces of code.

    Does anyone have any ideas how to do this?

    Thanks.

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

    (@bcworkz)

    Your attachment query could be replaced with get_attached_media(). It’s basically a wrapper for nearly the same query. Replace the title and link output for posts with this for attachments:

    <?php $attachments = get_attached_media( 'image' );
    if ( $attachments ) {
       foreach ( $attachments as $attachment ) { ?>
       <a href="<?php echo wp_get_attachment_url( $attachment->ID, true ); ?>"><?php the_title(); ?></a><br />
    <?php } ?>

    I’m assuming you want image attachments, you can replace ‘image’ with any valid mime type. I believe a comma separated list of types will also work, and an empty string would get all mime types, but I’ve not verified this.

    The exact code you’re replacing in two places is this:

    <a href="<?php the_permalink() ?>">
    <?php the_title(); ?>
    </a>
    Thread Starter jtm12

    (@jtm12)

    Thank you. Very helpful.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Get attachment for custom post with parent and child taxonomies’ is closed to new replies.