maxworkingwell

drama otaku display table on person page

Sep 17th, 2024
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.17 KB | None | 0 0
  1. // display table on person page
  2. function render_person_data( $atts ) {
  3.     $atts = shortcode_atts( array(
  4.         'child_id' => '',
  5.     ), $atts );
  6.  
  7.     $child_id = intval( $atts['child_id'] );
  8.  
  9.     if ( ! $child_id ) {
  10.         return '<strong>No child ID provided</strong>';
  11.     }
  12.  
  13.     $current_person_data = get_post_meta( $child_id, 'person_data', true );
  14.  
  15.     if ( empty( $current_person_data ) ) {
  16.         return '<strong>No items found</strong>';
  17.     }
  18.  
  19.     // Сортируем данные по полю 'first_air_date' в порядке убывания
  20.     usort( $current_person_data, function( $a, $b ) {
  21.         $first_air_date_a = get_post_meta( $a['parent_id'], 'wpcf-first-air-date', true );
  22.         $first_air_date_b = get_post_meta( $b['parent_id'], 'wpcf-first-air-date', true );
  23.         return $first_air_date_b - $first_air_date_a; // Сортировка по убыванию
  24.     });
  25.  
  26.     ob_start();
  27.     ?>
  28.     <table width="100%" class="people-credits-table">
  29.         <thead>
  30.             <tr>
  31.                 <th>Title</th>
  32.                 <th class="type">Type</th>
  33.                 <th>Release Date</th>
  34.             </tr>
  35.         </thead>
  36.         <tbody class="wpv-loop js-wpv-loop">
  37.         <?php foreach ( $current_person_data as $person_data ) :
  38.             $parent_id = $person_data['parent_id'];
  39.             $intermediary_id = $person_data['intermediary_id'];
  40.             $relation = $person_data['relation'];
  41.  
  42.             if ( in_array( $relation, array( 'cast', 'cast-movie', 'cast-special' ) ) ) {
  43.                 $relation_type = get_post_type( $parent_id );
  44.             } elseif ( in_array( $relation, array( 'crew-drama', 'crew-movie', 'crew-special' ) ) ) {
  45.                 $relation_type = 'crew';
  46.             } else {
  47.                 continue;
  48.             }
  49.            
  50.             $parent_title = get_the_title( $parent_id );
  51.             $parent_link = get_permalink( $parent_id );
  52.             $parent_type = get_post_type( $parent_id );
  53.             $featured_image = get_the_post_thumbnail( $parent_id, 'thumbnail' );
  54.             $first_air_date = get_post_meta( $parent_id, 'wpcf-first-air-date', true );
  55.             $average_rating = get_post_meta( $parent_id, 'wpcf-average-rating', true );
  56.             $country_terms = get_the_terms( $parent_id, 'country' );
  57.             $country_slug = $country_terms ? $country_terms[0]->slug : '';
  58.             $country_name = $country_terms ? $country_terms[0]->name : '';
  59.             $country_link = $country_terms ? get_term_link( $country_terms[0] ) : '';
  60.            
  61.             $role = get_post_meta( $intermediary_id, 'wpcf-role', true );
  62.             $role_importance = types_render_field( "role-importance", array( "item" => $intermediary_id ) );
  63.             $credits = types_render_field( "credits", array( "separator" => ", ", "item" => $intermediary_id ) );
  64.             ?>
  65.             <tr data-type="<?php echo esc_attr( $relation_type ); ?>" data-timestamp="<?php echo esc_attr( $first_air_date ); ?>" data-title="<?php echo esc_attr( $parent_title ); ?>">
  66.                 <td>
  67.                     <div class="d-flex">
  68.                         <div class="item-img"><?php echo $featured_image; ?></div>
  69.                         <div class="item-info">
  70.                             <a href="<?php echo esc_url( $parent_link ); ?>"><?php echo esc_html( $parent_title ); ?></a>
  71.                             <ul class="bycategories d-flex flex-wrap align-items-center">
  72.                                 <?php if ( !empty( $average_rating ) ) : ?>
  73.                                     <li style="font-size:13px;margin:0 5px 5px">
  74.                                         <i class="fas fa-star"></i> <?php echo esc_html( $average_rating ); ?>
  75.                                     </li>
  76.                                 <?php endif; ?>
  77.                                 <li>
  78.                                     <span class="sp-badge <?php echo esc_attr( $country_slug ); ?>">
  79.                                         <a href="<?php echo esc_url( $country_link ); ?>"><?php echo esc_html( $country_name ); ?></a>
  80.                                     </span>
  81.                                 </li>
  82.                                 <li style="margin-bottom:5px;width:100%;">
  83.                                     <?php echo esc_html( $role_importance ); ?>
  84.                                     <?php echo esc_html( $credits ); ?>
  85.                                     <?php echo !empty( $role ) ? ': ' . esc_html( $role ) : ''; ?>
  86.                                 </li>
  87.                             </ul>
  88.                         </div>
  89.                     </div>
  90.                 </td>
  91.                 <td class="type">
  92.                     <div class="p-type <?php echo esc_attr( $parent_type ); ?>" style="padding:3.3px 5px;margin:0 5px 5px 0">
  93.                         <i class="fas"></i> <?php echo esc_html( $parent_type ); ?>
  94.                     </div>
  95.                 </td>
  96.                 <td><?php echo date( 'M j, Y', $first_air_date ); ?></td>
  97.             </tr>
  98.         <?php endforeach; ?>
  99.         </tbody>
  100.     </table>
  101.     <?php
  102.  
  103.     return ob_get_clean();
  104. }
  105.  
  106. add_shortcode( 'person-data', 'render_person_data' );
Advertisement
Add Comment
Please, Sign In to add comment