Advertisement
salmancreation

Display Related Posts in WordPress without a Plugin

Apr 18th, 2020
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.71 KB | None | 0 0
  1. <?php
  2.  
  3. $related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'numberposts' => 5, 'post__not_in' => array($post->ID) ) );
  4. if( $related ) foreach( $related as $post ) {
  5. setup_postdata($post); ?>
  6.  <ul>
  7.         <li>
  8.         <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
  9.             <?php the_content('Read the rest of this entry &raquo;'); ?>
  10.         </li>
  11.     </ul>  
  12. <?php }
  13.  
  14. -------=--=-=-=-
  15.  
  16. <?php
  17.                         $related = new WP_Query(
  18.                           array(
  19.                          'category__in'   => wp_get_post_categories( $post->ID ),
  20.                          'posts_per_page' => 2,
  21.                          'post__not_in'   => array( $post->ID )
  22.                           )
  23.                        );
  24.  
  25.                         $user = wp_get_current_user();
  26.                         if( $related->have_posts() ) {
  27.                         while ($related->have_posts()) : $related->the_post(); ?>
  28.                          <div class="col-xl-6 col-md-6 col-12">
  29.                             <div class="single-blog-item">
  30.                                 <div class="featured-thumb">
  31.                                     <img src="<?php the_post_thumbnail(); ?>" alt="<?php the_title(); ?>">
  32.                                 </div>
  33.                                 <?php if( '1' == $is_post_date): ?>
  34.                                 <span class="post-date"><?php the_time(get_option('date_format')) ?></span>
  35.                                 <?php endif; ?>
  36.                                 <div class="post-meta">
  37.                                     <div class="post-athuor">
  38.                                         <?php if ($user) : ?>
  39.                                         <div class="author-img" style="    background-image: url(<?php echo esc_url(get_avatar_url($user->ID)); ?>)"></div>
  40.                                         <?php endif; ?>
  41.                                         <a href="<?php echo get_author_posts_url(get_the_author_meta('ID'), get_the_author_meta('user_nicename')); ?>"><?php the_author(); ?></a>
  42.                                     </div>
  43.                                     <div class="post-comment">
  44.                                         <a href="<?php the_permalink() ?>"><i class="far fa-comment-dots"></i><?php echo get_comments_number(); ?> Comments</a>
  45.                                     </div>
  46.                                 </div>
  47.                                 <div class="post-content">
  48.                                     <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
  49.                                     <p><?php echo strip_shortcodes(saasnext_excerpt('saasnext_opt', 'blog_excerpt', true)); ?></p>
  50.                                     <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>" class="read-btn">Read More <i class="far fa-long-arrow-alt-right"></i></a>
  51.                                 </div>
  52.                             </div>
  53.                         </div>
  54.                         <?php
  55.                         endwhile;
  56.                         wp_reset_postdata();
  57.                         }
  58.                     ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement