Advertisement
salmancreation

Perfect Related post by Category without plugin

Jan 18th, 2015
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.68 KB | None | 0 0
  1.  
  2.  
  3.  
  4. <?php $orig_post = $post;
  5. global $post;
  6. $categories = get_the_category($post->ID);
  7. if ($categories) {
  8. $category_ids = array();
  9. foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
  10. $args=array(
  11. 'category__in' => $category_ids,
  12. 'post__not_in' => array($post->ID),
  13. 'posts_per_page'=> 5, // Number of related posts that will be displayed.
  14. 'caller_get_posts'=>1,
  15. 'orderby'=>'rand' // Randomize the posts
  16. );
  17. $my_query = new wp_query( $args );
  18. if( $my_query->have_posts() ) {
  19. echo '<div id="perfect-related_by-category" class="clear"><h3>Related Posts</h3><ul>';
  20. while( $my_query->have_posts() ) {
  21. $my_query->the_post(); ?>
  22. <li>
  23.  <a href="<?php the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>">
  24.  </a>
  25.  <div class="perfect-related_by-category">
  26.  <a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
  27.  </div>
  28. </li>
  29. <? }
  30. echo '</ul></div>';
  31. } }
  32. $post = $orig_post;
  33. wp_reset_query(); ?>
  34.  
  35.  
  36. Also put this code in style.css
  37.  
  38. /*Perfect Related post by Category without plugin*/
  39.  
  40. .custom #perfect-related_by-category ul {
  41.  margin:0;
  42.  overflow:hidden;
  43. }
  44. .custom #perfect-related_by-category li {
  45.  float: left;
  46.  list-style: none;
  47.  margin: 0 0 0 20px;
  48. }
  49. .custom #perfect-related_by-category li:first-child {
  50.  margin-left: 0;
  51. }
  52. .custom #perfect-related_by-category li a {
  53.  display: block;
  54.  font-size: 12px;
  55.  line-height: 16px;
  56.  text-align: left;
  57.  text-decoration:none;
  58.  width: 110px;
  59. }
  60. .custom #perfect-related_by-category img {
  61.  border: 2px solid #DDDDDD;
  62.  height: 110px;
  63.  width: 110px;
  64. }
  65. .custom #perfect-related_by-category li a:hover {
  66.  text-decoration: underline;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement