Advertisement
Barbareshet

related short code

Aug 22nd, 2017
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function widget_shortcode() {
  2.  
  3.     global $post;
  4.  
  5. $current_post_type = get_post_type( $post );
  6.  
  7. // The query arguments
  8. $args = array(
  9.     'posts_per_page' => 3,
  10.     'order' => 'DESC',
  11.     'orderby' => 'ID',
  12.     'post_type' => $current_post_type,
  13.     'post__not_in' => array( $post->ID )
  14. );
  15.  
  16. // Create the related query
  17. $rel_query = new WP_Query( $args );
  18.  
  19. // Check if there is any related posts
  20. if( $rel_query->have_posts() ) :
  21. ob_start();
  22. ?>
  23. <h4 class="widget-title toprelated">אולי יעניין אותך גם</h4>
  24.    <div id="related" class="group">
  25.     <ul class="group">
  26. <?php
  27.     // The Loop
  28.     while ( $rel_query->have_posts() ) :
  29.         $rel_query->the_post();
  30. ?>
  31.         <li>
  32.         <a href="<?php the_permalink() ?>" title="<?php the_title() ?>" rel="bookmark">
  33.             <article>
  34.                  <div class="thethumbnail"><?php the_post_thumbnail( array(300, 250) ); ?></div>
  35.                 <h4 class="entry-title"><?php the_title() ?></h4>
  36.              
  37.             </article>
  38.         </a>
  39.         </li>
  40. <?php
  41.     endwhile;
  42. ?>
  43.     </ul><!-- .group -->
  44. </div><!-- #related -->
  45. <?php
  46. return ob_get_clean();
  47. endif;
  48.  
  49. // Reset the query
  50. wp_reset_query();
  51.  
  52.  
  53.  }    
  54. add_shortcode( 'related', 'widget_shortcode' );
  55.  
  56. //run shortcode in text widget
  57.  
  58. add_filter('widget_text','do_shortcode');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement