Advertisement
arnabkumar

related post in wordpress

Jun 11th, 2014
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.98 KB | None | 0 0
  1. <?php
  2. /* related post in wordpress by Category */
  3.  
  4. /* 1. add this code in functions.php */
  5.  
  6.  
  7.     add_theme_support( 'post-thumbnails' );  
  8.     set_post_thumbnail_size( 150, 150, true );  
  9.    
  10.    
  11.  
  12.    
  13. /* Then add this codode in single.php */
  14.  
  15.  <div class="relatedposts">
  16. <h3>Related posts</h3>
  17. <?php $orig_post = $post;
  18.     global $post;
  19.     $categories = get_the_category($post->ID);
  20.     if ($categories) {
  21.     $category_ids = array();
  22.     foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
  23.    
  24.     $args=array(
  25.     'category__in' => $category_ids,
  26.     'post__not_in' => array($post->ID),
  27.     'posts_per_page'=> 4, // Number of related posts that will be shown.
  28.     'caller_get_posts'=>1
  29.     );
  30.    
  31.     $my_query = new wp_query( $args );
  32.  
  33.     while( $my_query->have_posts() ) {
  34.     $my_query->the_post();
  35.     ?>
  36.    
  37.     <div class="relatedthumb">
  38.         <a rel="external" href="<?php the_permalink()?>"><?php the_post_thumbnail(array(150,150)); ?><br />
  39.         <?php the_title(); ?>
  40.         </a>
  41.     </div>
  42.    
  43.     <?php }
  44.     }
  45.     $post = $orig_post;
  46.     wp_reset_query();
  47.     ?>
  48. </div>
  49.  
  50.  
  51. /* 3 Some css style */
  52. .relatedposts {width: 640px; margin: 0 0 20px 0; float: left; font-size: 12px;}
  53. .relatedposts h3 {font-size: 20px; margin: 0 0 5px 0; }
  54. .relatedthumb {margin: 0 1px 0 1px; float: left; }
  55. .relatedthumb img {margin: 0 0 3px 0; padding: 0;}
  56. .relatedthumb a {color :#333; text-decoration: none; display:block; padding: 4px; width: 150px;}
  57. .relatedthumb a:hover {background-color: #ddd; color: #000;}
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.                               /* related post in wordpress by tag */
  65.                              
  66.                              
  67. /* 1. add this code in functions.php */
  68.  
  69.  
  70.     add_theme_support( 'post-thumbnails' );  
  71.     set_post_thumbnail_size( 150, 150, true );
  72.    
  73.    
  74. /* 2 Then add this codode in single.php */
  75.  
  76. <div class="relatedposts">
  77. <h3>Related posts</h3>
  78. <?php
  79.     $orig_post = $post;
  80.     global $post;
  81.     $tags = wp_get_post_tags($post->ID);
  82.    
  83.     if ($tags) {
  84.     $tag_ids = array();
  85.     foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
  86.     $args=array(
  87.     'tag__in' => $tag_ids,
  88.     'post__not_in' => array($post->ID),
  89.     'posts_per_page'=>4, // Number of related posts to display.
  90.     'caller_get_posts'=>1
  91.     );
  92.    
  93.     $my_query = new wp_query( $args );
  94.  
  95.     while( $my_query->have_posts() ) {
  96.     $my_query->the_post();
  97.     ?>
  98.    
  99.     <div class="relatedthumb">
  100.         <a rel="external" href="<?php the_permalink()?>"><?php the_post_thumbnail(array(150,150)); ?><br />
  101.         <?php the_title(); ?>
  102.         </a>
  103.     </div>
  104.    
  105.     <?php }
  106.     }
  107.     $post = $orig_post;
  108.     wp_reset_query();
  109.     ?>
  110. </div>
  111.  
  112.  
  113. /* 3 Some css style */
  114. .relatedposts {width: 640px; margin: 0 0 20px 0; float: left; font-size: 12px;}
  115. .relatedposts h3 {font-size: 20px; margin: 0 0 5px 0; }
  116. .relatedthumb {margin: 0 1px 0 1px; float: left; }
  117. .relatedthumb img {margin: 0 0 3px 0; padding: 0;}
  118. .relatedthumb a {color :#333; text-decoration: none; display:block; padding: 4px; width: 150px;}
  119. .relatedthumb a:hover {background-color: #ddd; color: #000;}
  120. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement