Support

Account

Home Forums General Issues Getting file field URL from term child

Unread

Getting file field URL from term child

  • I’m trying to provide a direct download link on a page that displays posts from categories in a custom taxonomy. I have everything working except the link, which currently just links to the main page as my $url variable is not working. Where have I gone wrong?:

    $term = get_queried_object();
    					$term_id = $term->term_id;
    					$taxonomy_name = $term->taxonomy;
    
    					$termchildren = get_term_children( $term_id, $taxonomy_name );
    
    					echo '<ul>';
    					    foreach ( $termchildren as $child ) {
    					        $childTerm = get_term_by('id', $child, $term->taxonomy );
    					        echo '<li><a href="' . get_term_link( $childTerm, $term->taxonomy ) . '">' . $childTerm->name . '</a>';
    
    					        // posts with this term         
    					        $childTermPosts = get_posts(array(
    					          'post_type' => 'cpt_downloads', // WTV your post_type is for Downloads tax
    					          'numberposts' => -1,
    					          'tax_query' => array(
    					            array(
    					              'taxonomy' => $taxonomy_name,
    					              'field' => 'id',
    					              'terms' => $child,
    					              'include_children' => true
    					              )
    					            )
    					        ));
    
    					        // then loops the posts
    					        if (count($childTermPosts) > 0) {
    					            echo "<ul>";
    
    					            foreach ($childTermPosts as $childTermPost) { 
    
    									$file = get_field('download', $childTermPost->ID);
    
    									if( $file ) {
    
    										$url = wp_get_attachment_url( $file );
    
    										echo "<li><a href='".$url."'>{$childTermPost->post_title}</a></li>";
    									}
    									
    								}
    								
    					            echo "</ul>";
    					        }
    					
    					        echo "</li>";
    					
    					    } //end foreach
    
    					    echo '</ul>';
Viewing 1 post (of 1 total)

The topic ‘Getting file field URL from term child’ is closed to new replies.