Guest User

Untitled

a guest
Feb 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. <?php
  2. // Default arguments
  3. $args = array(
  4. 'posts_per_page' => 4, // How many items to display
  5. 'post__not_in' => array( get_the_ID() ), // Exclude current post
  6. 'no_found_rows' => true, // We don't ned pagination so this speeds up the query
  7. );
  8.  
  9. // Check for current post category and add tax_query to the query arguments
  10. $cats = wp_get_post_terms( get_the_ID(), 'category' );
  11. $cats_ids = array();
  12. foreach( $cats as $wpex_related_cat ) {
  13. $cats_ids[] = $wpex_related_cat->term_id;
  14. }
  15. if ( ! empty( $cats_ids ) ) {
  16. $args['category__in'] = $cats_ids;
  17. }
  18.  
  19. // Query posts
  20. $wpex_query = new wp_query( $args );
  21.  
  22. // Loop through posts
  23. foreach( $wpex_query->posts as $post ) : setup_postdata( $post ); ?>
  24.  
  25. <a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>"><?php the_title(); ?></a>
  26.  
  27. <?php
  28. // End loop
  29. endforeach;
  30.  
  31. // Reset post data
  32. wp_reset_postdata(); ?>
Add Comment
Please, Sign In to add comment