1

I just need an idea where to put this code:

$term_featured_image_id = get_term_meta( $custom_term->ID, 'wp_travel_trip_type_image_id', true );
    $image_url = wp_get_attachment_url( $term_featured_image_id );

In This code:

<?php
$post_type = 'itineraries';
$custom_tax = 'travel_locations';
$custom_args = array(
'parent' => 0
);
$custom_terms = get_terms($custom_tax, $custom_args);
                    foreach($custom_terms as $custom_term) {
                        $term_link = get_term_link( $custom_term  );
                        wp_reset_query();
                        $args = array('post_type' => 'itineraries',
                                      'tax_query' => array(
                                          array(                                                  
                                              'taxonomy' => 'travel_locations',
                                              'field' => 'slug',
                                              'terms' => $custom_term->slug,                                                  

                                              ),
                                          ),
                                     ); ?>
                    <?php 
                        $loop = new WP_Query($args);
                        if($loop->have_posts()) { ?>
<?php
while($loop->have_posts()) : $loop->the_post(); ?>
//some content here
<?php
endwhile;?>
<?php } }?>

1 Answer 1

1

Here's an example how you could use your first code in the second one. In my code I also took the liberty to suggest one way of making your code more clear and easier to read by splitting the code into separate functions.

// add the following helper functions to functions.php
function travel_trip_type_image_url( int $term_id ) {
  $img_id = get_term_meta( $term_id, 'wp_travel_trip_type_image_id', true );
  return ( $img_id && is_int($img_id) ) ? wp_get_attachment_url( $img_id ) : '';
}

function travel_location_terms() {
  $terms = get_terms( array(
    'taxonomy'   => 'travel_locations',
    'hide_empty' => false,
    'parent'     => 0
  ) );
  return ( $terms && ! is_wp_error($terms) ) ? $terms : array();
}

function itineraries_query( string $term ) {
  $args = array(
    'post_type' => 'itineraries',
    'tax_query' => array(
      array(                                                  
        'taxonomy' => 'travel_locations',
        'field'    => 'slug',
        'terms'    => $term,
      ),
    ),
  );
  return new WP_Query($args);
}

function itineraries_loop( WP_Query $query ) {
  if ( $query->have_posts() ) {
    while( $query->have_posts() ) {      
      $query->the_post();
      the_title('<h2>','</h2>');
      // use get_template_part( 'your-post-entry-template' ); 
      // or write html markup here
    }    
    wp_reset_postdata();    
  }
}

function travel_type_header( WP_Term $term ) {
  if ( $term_img_url = travel_trip_type_image_url( $term->term_id ) ) {
    // modify html output as needed
    printf(
      '<a href="%s"><img src="%s" alt="%s"></a>',
      esc_url( get_term_link( $term ) ),
      esc_url( $term_img_url ),
      esc_attr( $term->name )
    );
  }
}

// use in some template file
foreach ( travel_location_terms() as $travel_location ) {
  travel_type_header( $travel_location );
  itineraries_loop( itineraries_query( $travel_location->slug ) );  
}

But if you don't want to use the suggested split, then just put your first code between the foreach and $term_link lines.

To use this code you need to

  1. check and, if needed, update the meta key used in travel_trip_type_image_url(),
  2. check and, if needed, update taxonomy and post type names used in code,
  3. update the loop entry html output inside itineraries_loop() to your liking,
  4. update the term header html output inside travel_type_header() to your liking,
  5. have terms and post in your custom taxonomy and post type.
10
  • Thank you sir for help, but please define this function without putting in function.php. I tried with function.php but it's not worked! Commented Nov 16, 2019 at 13:51
  • @KaranKcoresys In the case you don't want to use the code example I posted, just read the last line of my answer. You should be able to just add your first code inside the foreach loop of your second code. Commented Nov 16, 2019 at 18:19
  • @KaranKcoresys Could you be more specific with the code "not working", please? What did you do with the code exactly? Did you just copy-paste it or tweaked it to match your setup? Did you get any output? Or errors? Commented Nov 16, 2019 at 18:22
  • thank you sir for the reply, I am using the plugin named wptravel. i tried many things but not able to get taxonomy featured image in to the page. I used to put first code after foreach. but i don't know how to call image in the image tag. <img src="<?php echo esc_url( $image_url[0] ); ?>"> Commented Nov 18, 2019 at 5:16
  • @KaranKcoresys If you used $image_url = wp_get_attachment_url( $term_featured_image_id ); to get the image url, then you can use <img src="<?php echo esc_url( $image_url[0] ); ?>"> as the url function returns either the image url as a string or a boolean false. Of course you can wrap the img in a ifstatement so that the img tag is not printed for nothing, if the function returns false and there's no url. Commented Nov 18, 2019 at 7:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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