Advertisement
niammuddin

RelatedPost Wordpress by category

Jul 27th, 2017
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.82 KB | None | 0 0
  1. <!-- related post -->
  2. <?php
  3. $orig_post = $post;
  4. global $post;
  5. $categories = get_the_category($post->ID);
  6. if ($categories) {
  7. $category_ids = array();
  8. foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
  9. $args=array(
  10. 'category__in' => $category_ids,
  11. 'post__not_in' => array($post->ID),
  12. 'posts_per_page'=> 10, // Number of related posts that will be shown.
  13. 'caller_get_posts'=>1
  14. );
  15. $my_query = new wp_query( $args );
  16. if( $my_query->have_posts() ) {
  17. echo '<div class="sidebar-widget"><div class="title_sidebar">Artikel Terkait</div><ul>';
  18. while( $my_query->have_posts() ) {
  19. $my_query->the_post();?>
  20. <li><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
  21. <?
  22. }
  23. echo '</ul></div>';
  24. }
  25. }
  26. $post = $orig_post;
  27. wp_reset_query();
  28. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement