custom taxonomy - How can marge these loop code?

I just need an idea where to put this code:$term_featured_image_id = get_term_meta( $custom_term->ID, 'wp_travel

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 } }?>

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 } }?>
Share Improve this question asked Nov 16, 2019 at 12:20 Karan KcoresysKaran Kcoresys 135 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744994505a4605106.html

相关推荐

  • custom taxonomy - How can marge these loop code?

    I just need an idea where to put this code:$term_featured_image_id = get_term_meta( $custom_term->ID, 'wp_travel

    17小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信