Advertisement
artemsemkin

Rubenz navigation for portfolio Items of the same category

Jan 24th, 2021
1,442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.07 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Get the term names of taxonomy
  5.  *
  6.  * @param string $post_id
  7.  * @param string $taxonomy
  8.  * @return void
  9.  */
  10. function arts_get_taxonomy_term_names( $post_id, $taxonomy ) {
  11.   $items = get_the_terms( $post_id, $taxonomy );
  12.   $arr   = [];
  13.  
  14.   if ( is_array( $items ) ) {
  15.     foreach ( $items as $item ) {
  16.  
  17.       $temp = array(
  18.         'slug' => $item->slug,
  19.         'name' => $item->name,
  20.       );
  21.  
  22.       // don't add the same item multiple times
  23.       if ( ! in_array( $arr, $temp ) ) {
  24.         array_push( $arr, $temp );
  25.       }
  26.     }
  27.   }
  28.  
  29.   return $arr;
  30. }
  31.  
  32. $next_post  = get_previous_post( true, '', 'arts_portfolio_category' );
  33. $prev_post  = get_next_post( true, '', 'arts_portfolio_category' );
  34. $current_id = get_the_ID();
  35. $next_link;
  36. $next_title;
  37. $next_img;
  38. $prev_link;
  39. $prev_title;
  40. $prev_img;
  41. $attr_link                          = '';
  42. $class_link                         = 'col';
  43. $class_link_prev                    = '';
  44. $prev_label                         = get_theme_mod( 'portfolio_nav_prev_title', esc_html__( 'Prev', 'rubenz' ) );
  45. $next_label                         = get_theme_mod( 'portfolio_nav_next_title', esc_html__( 'Next', 'rubenz' ) );
  46. $enable_portfolio_loop              = get_theme_mod( 'enable_portfolio_loop', true );
  47. $enable_portfolio_next_first_mobile = get_theme_mod( 'enable_portfolio_next_first_mobile', false );
  48.  
  49. $categories         = array();
  50. $current_categories = arts_get_taxonomy_term_names( $current_id, 'arts_portfolio_category' );
  51. foreach ( $current_categories as $item ) {
  52.   array_push( $categories, $item['slug'] );
  53. }
  54.  
  55. $args  = array(
  56.   'post_type'      => 'arts_portfolio_item',
  57.   'posts_per_page' => -1,
  58.   'tax_query'      => array(
  59.     array(
  60.       'taxonomy' => 'arts_portfolio_category',
  61.       'field'    => 'slug',
  62.       'terms'    => $categories, // include only posts of the current categories (slugs)
  63.     ),
  64.   ),
  65. );
  66. $posts = get_posts( $args );
  67.  
  68. $first_post = current( $posts );
  69. $last_post  = end( $posts );
  70.  
  71. if ( $next_post ) {
  72.  
  73.   $next_link  = get_permalink( $next_post );
  74.   $next_title = $next_post->post_title;
  75.   $next_img   = get_post_thumbnail_id( $next_post->ID );
  76. }
  77.  
  78. if ( ! $next_post && $enable_portfolio_loop ) {
  79.  
  80.   $next_post  = $first_post;
  81.   $next_link  = get_permalink( $next_post );
  82.   $next_title = $next_post->post_title;
  83.   $next_img   = get_post_thumbnail_id( $next_post->ID );
  84.  
  85. }
  86.  
  87. if ( $prev_post ) {
  88.  
  89.   $prev_link  = get_permalink( $prev_post );
  90.   $prev_title = $prev_post->post_title;
  91.   $prev_img   = get_post_thumbnail_id( $prev_post->ID );
  92.  
  93. }
  94.  
  95. if ( ! $prev_post && $enable_portfolio_loop ) {
  96.  
  97.   $prev_post  = $last_post;
  98.   $prev_link  = get_permalink( $prev_post );
  99.   $prev_title = $prev_post->post_title;
  100.   $prev_img   = get_post_thumbnail_id( $prev_post->ID );
  101.  
  102. }
  103.  
  104. if ( $next_post && $prev_post ) {
  105.   $class_link = 'col-md-6';
  106. }
  107.  
  108. if ( $enable_portfolio_next_first_mobile ) {
  109.   $class_link_prev = 'order-md-0 order-2';
  110. }
  111.  
  112. ?>
  113.  
  114. <?php if ( $next_post || $prev_post ) : ?>
  115.   <!-- section NAV PROJECTS -->
  116.   <section class="section section-nav-projects container-fluid no-gutters">
  117.     <div class="row no-gutters h-100">
  118.       <?php if ( $prev_post ) : ?>
  119.         <?php
  120.         if ( $prev_img ) {
  121.           $attr_link = 'data-pjax-link=navProjects';
  122.         }
  123.         ?>
  124.         <a class="section-nav-projects__inner section-nav-projects__inner_prev <?php echo esc_attr( $class_link ); ?> <?php echo esc_attr( $class_link_prev ); ?>" href="<?php echo esc_url( $prev_link ); ?>" <?php echo esc_attr( $attr_link ); ?> data-post-id="<?php echo esc_attr( $prev_post->ID ); ?>">
  125.           <?php if ( ! empty( $prev_label ) ) : ?>
  126.             <div class="section-nav-projects__label section-nav-projects__label_prev"><?php echo esc_html( $prev_label ); ?></div>
  127.           <?php endif; ?>
  128.           <div class="section-nav-projects__arrow"><i class="material-icons">keyboard_arrow_left</i></div>
  129.           <h3><?php echo esc_html( $prev_title ); ?></h3>
  130.         </a>
  131.       <?php endif; ?>
  132.       <?php if ( $next_post ) : ?>
  133.         <?php
  134.         if ( $next_img ) {
  135.           $attr_link = 'data-pjax-link=navProjects';
  136.         }
  137.         ?>
  138.         <a class="section-nav-projects__inner section-nav-projects__inner_next <?php echo esc_attr( $class_link ); ?>" href="<?php echo esc_url( $next_link ); ?>" <?php echo esc_attr( $attr_link ); ?> data-post-id="<?php echo esc_attr( $next_post->ID ); ?>">
  139.           <?php if ( ! empty( $next_label ) ) : ?>
  140.             <div class="section-nav-projects__label section-nav-projects__label_next"><?php echo esc_html( $next_label ); ?></div>
  141.           <?php endif; ?>
  142.           <h3><?php echo esc_html( $next_title ); ?></h3>
  143.           <div class="section-nav-projects__arrow"><i class="material-icons">keyboard_arrow_right</i></div>
  144.         </a>
  145.       <?php endif; ?>
  146.     </div>
  147.     <div class="section-nav-projects__backgrounds">
  148.       <?php if ( $prev_post && ! empty( $prev_img ) ) : ?>
  149.         <?php
  150.           arts_the_lazy_image(
  151.             array(
  152.               'id'        => $prev_img,
  153.               'class'     => array(
  154.                 'image' => array( 'section-nav-projects__background', 'section-nav-projects__background_prev' ),
  155.               ),
  156.               'attribute' => array(
  157.                 'image' => array( 'data-background-for=' . $prev_post->ID ),
  158.               ),
  159.             )
  160.           );
  161.         ?>
  162.       <?php endif; ?>
  163.       <?php if ( $next_post && ! empty( $next_img ) ) : ?>
  164.         <?php
  165.           arts_the_lazy_image(
  166.             array(
  167.               'id'        => $next_img,
  168.               'class'     => array(
  169.                 'image' => array( 'section-nav-projects__background', 'section-nav-projects__background_next' ),
  170.               ),
  171.               'attribute' => array(
  172.                 'image' => array( 'data-background-for=' . $next_post->ID ),
  173.               ),
  174.             )
  175.           );
  176.         ?>
  177.       <?php endif; ?>
  178.       <div class="overlay overlay_dark section-nav-projects__overlay"></div>
  179.     </div>
  180.   </section>
  181.   <!-- - section NAV PROJECTS -->
  182. <?php endif; ?>
  183.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement