0

I need to echo a term of a CPT in this loop after the

  • for each item in the list. Any direction or help is appreciated. Original code is from Stackoverflow.com because it worked.

     <?php
    $da_place = get_field('smart_place'); //acf field
    
        $args = array(
            'post_type' => 'to_do_items',
            'tax_query' => array(
                relation => 'AND',
                array(
                    'taxonomy' => 'days',
                    'field'    => 'slug',
                    'terms'    => 'tuesday',
                ),
                array(
                    'taxonomy' => 'places',
                    'field'    => 'slug',
                    'terms'    => $da_place,
                ),
            ),
        );
        $the_query = new WP_Query( $args );    
    ?> Tuesday <?
    
    // The Loop
    if ( $the_query->have_posts() ) {
    
        echo '<ul>';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
    
            echo '<li>' . get_the_title() . '';
            echo  '</li>' ;
    echo $specialTermHere;
        }echo '</ul>';
        /* Restore original Post Data */
        wp_reset_postdata();
    } else {
        echo 'Nothing Today';
        // no posts found
    } 
    
    //end of Tues
    ?>
    
  • 0

    I think you want to get the term link of the of each post. I will explain how you should get the answer for this.

    First you will need get the term object of current post. Support your taxonomy name is 'your_taxonomy';

    $terms = get_the_terms( $post_id, 'your_taxonomy' );
    

    Because $term here is an array, in most case you will need the first one:

    $term = $terms[0];
    

    Then you can get the link to your term by using get_term_link function. You can use printf to make code easier to read:

    printf(
        '<a href="%1$s" title="%2$s">%2$s</a>',
        esc_url( get_term_link( $term ) ),
        esc_html( $term->name )
    );
    

    Because you will you this code in a loop, it makes sense to wrap all the logic in a function with two parameters (post ID and taxonomy name).

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

    Not the answer you're looking for? Browse other questions tagged or ask your own question.