iftekharul

Related posts both for regular posts and custom type posts

Mar 11th, 2015
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.68 KB | None | 0 0
  1. <?php
  2. $post_type = get_post_type();
  3. if($post_type == 'post') {
  4.     $categories = get_the_category($post->ID);
  5. } else {
  6.     $taxonomy = get_object_taxonomies($post_type);
  7.     $categories = get_the_terms( $post->ID, $taxonomy );
  8. }
  9. if ($categories) {
  10.     if($post_type == 'post') {
  11.         $category_ids = array();
  12.         foreach($categories as $individual_category) { $category_ids[] = $individual_category->term_id; }
  13.         $args = array( 'category__in' => $category_ids, 'post__not_in' => array($post->ID), 'posts_per_page' => 4, 'ignore_sticky_posts' => 1 );
  14.     } else {
  15.         foreach($categories as $term) { $post_tax = $term->taxonomy; }
  16.         $args = array( 'post_type' => $post_type, "$post_tax" => $term->slug, 'post__not_in' => array($post->ID), 'posts_per_page' => 4);
  17.     }
  18.     $my_query = new wp_query($args);
  19.     if( $my_query->have_posts() ) { ?>
  20.         <div class="t4b_related_tips"> <!-- Related Post Start -->
  21.             <div class="tips-head"><h2>এছাড়াও আপনি পছন্দ করতে পারেন</h2></div>
  22.             <ul><?php
  23.                 while ($my_query->have_posts()) : $my_query->the_post(); ?>
  24.                     <li><?php
  25.                         if ( has_post_thumbnail() ) { ?>
  26.                             <div class="tips-image"><a href="<?php the_permalink() ?>"><?php the_post_thumbnail('relatedpost-thumb'); ?></a></div><?php
  27.                         } else { ?>
  28.                             <div class="tips-image">
  29.                                 <a href="<?php the_permalink() ?>">
  30.                                     <img src="<?php echo catch_that_image(); ?>" alt="tips4blog"/>
  31.                                 </a>
  32.                             </div><?php
  33.                         } ?>
  34.                         <div class="tips-title"><h3><?php echo wp_trim_words( $post->post_title, 15, ' [...]' ); ?></h3></div>
  35.                     </li><?php
  36.                 endwhile; wp_reset_query(); ?>
  37.             </ul>
  38.         </div> <!-- Related Post End --><?php
  39.     }
  40. }
  41. ?>
Advertisement
Add Comment
Please, Sign In to add comment