cipher87

Related Posts using Categories

May 16th, 2021
604
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /**
  3.  * @link https://kriesi.at/support/topic/related-posts-not-showing/
  4.  */
  5. if ( ! defined( 'ABSPATH' ) ) {  exit;  }    // Exit if accessed directly
  6.  
  7. /**
  8.  *  These functions shows a number of posts related to the currently displayed post.
  9.  *  Relations are defined by categories: if post categories match, the post will be displayed as related
  10.  */
  11. global $avia_config;
  12.  
  13.  
  14. $rp = avia_get_option('single_post_related_entries');
  15.  
  16.  
  17.  
  18.  
  19. if(!isset($avia_config['related_posts_config']))
  20. {
  21.     $avia_config['related_posts_config'] = array(
  22.    
  23.     'columns' => 8,
  24.     'post_class' =>  "av_one_eighth no_margin ",
  25.     'image_size' => 'square',
  26.     'tooltip'    => true,
  27.     'title_short'=> false
  28.    
  29.     );
  30.    
  31.     if($rp == "av-related-style-full")
  32.     {
  33.         $avia_config['related_posts_config'] = array(
  34.    
  35.         'columns' => 6,
  36.         'post_class' =>  "av_one_half no_margin ",
  37.         'image_size' => 'square',
  38.         'tooltip'    => false, 
  39.         'title_short'=> true   
  40.         );
  41.     }
  42. }
  43.  
  44. if($rp == "disabled") return;
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52. extract($avia_config['related_posts_config']);
  53.  
  54.  
  55. $is_portfolio       = false; //avia_is_portfolio_single();
  56. $related_posts      = false;
  57. $this_id            = $post->ID;
  58. $slidecount         = 0;
  59. $postcount          = ($columns * 1);
  60. $format             = "";
  61. $fake_image         = "";
  62. $categories         = wp_get_post_categories($this_id);
  63.  
  64. if (!empty($categories) && is_array($categories))
  65. {
  66.      $category_ids = array();
  67.      foreach ($categories as $category ) {
  68.         $category_ids[] = (int)$category;
  69.     }
  70.  
  71.      if(!empty($category_ids))
  72.      {
  73.  
  74.  
  75.         $my_query = get_posts(
  76.                             array(
  77.                                 'category__in' => $category_ids,
  78.                                 'post_type' => get_post_type($this_id),
  79.                                 'showposts'=>$postcount, 'ignore_sticky_posts'=>1,
  80.                                 'orderby'=>'rand',
  81.                                 'post__not_in' => array($this_id))
  82.                             );
  83.  
  84.         if (!empty($my_query))
  85.         {
  86.             $extra = 'alpha';
  87.             $count = 1;
  88.             $output = "";
  89.  
  90.             //create seperator
  91.  
  92.             $output .= "<div class ='related_posts clearfix {$rp}'>";
  93.  
  94.  
  95.             $output .= "<h4 class='related_title'>".__('You might also like', 'avia_framework')."</h4>";
  96.             $output .= "<div class='related_entries_container '>";
  97.  
  98.             foreach ($my_query as $related_post)
  99.             {
  100.                 $related_posts = true;
  101.                 $slidecount ++;
  102.                 $format = "";
  103.                 if($is_portfolio) $format = "portfolio";
  104.                 if(!$format) $format = get_post_format($related_post->ID);
  105.                 if(!$format) $format = 'standard';
  106.                 if( ! empty( $title_short ) )
  107.                 {
  108.                     $related_post->post_title = wp_trim_words( avia_wp_get_the_title( $related_post->ID ), 17 );
  109.                 }
  110.                
  111.                 $post_thumbnail_id = get_post_thumbnail_id($related_post->ID);
  112.                 $post_thumb = get_the_post_thumbnail($related_post->ID, $image_size, array('title' => esc_attr(get_the_title($post_thumbnail_id))));
  113.                 $image      = $post_thumb ? $post_thumb : "<span class='related_posts_default_image'>{image}</span>";
  114.                 $fake_image = $post_thumb ? $post_thumb : $fake_image;
  115.                 $extra_class= $post_thumb ? "" : "related-format-visible";
  116.                 $parity     = $slidecount % 2 ? 'Odd' : 'Even';
  117.                 $insert_tooltip = $tooltip == true ? "data-avia-related-tooltip=\"". esc_attr( avia_wp_get_the_title( $related_post->ID ) ) . "\"" : "";
  118.                
  119.                 $output .= "<div class='$post_class $extra relThumb relThumb{$count} relThumb{$parity} post-format-{$format} related_column'>\n";
  120.                 $output .= "    <a href='".get_permalink($related_post->ID)."' class='relThumWrap noLightbox' title='".esc_attr( avia_wp_get_the_title( $related_post->ID ) )."'>\n";
  121.                 $output .= "    <span class='related_image_wrap' {$insert_tooltip}>";
  122.                 $output .=      $image;
  123.                 $output .= "    <span class='related-format-icon {$extra_class}'><span class='related-format-icon-inner' ".av_icon_string($format)."></span></span>";
  124.                 $output .= "    </span>";
  125.                 $output .=      "<strong class='av-related-title'>" . avia_wp_get_the_title( $related_post->ID ) . "</strong>";
  126.                 $output .=      apply_filters('avf_related_post_loop', "", $related_post);
  127.                 $output .= "    </a>";
  128.                 $output .= "</div>";
  129.  
  130.                 $count++;
  131.                 $extra = "";
  132.  
  133.                 if($count == count($my_query)) $extra = 'omega';
  134.  
  135.             }
  136.  
  137.  
  138.             $output .= "</div></div>";
  139.             $output = str_replace("{image}",$fake_image,$output);
  140.  
  141.             if($related_posts) echo $output;
  142.  
  143.         }
  144.  
  145.         wp_reset_query();
  146.     }
  147. }
  148.  
  149.  
RAW Paste Data