How to add related posts in theme
-
Hi, David Sir.
Thank you for this awesome theme. The theme is highly customizable but I’m struggling with putting related posts script in a single.php file. I tried many code snippets but none was helpful. I broke my sites 2 times while putting these codes. The code that I earlier used in another theme worked quite well but in PBF it broke the site. Here is the code.<?php if($page-builder-framework_relatedposts_section == '1') { ?> <!-- Start Related Posts --> <?php $categories = get_the_category($post->ID); if ($categories) { $category_ids = array(); foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id; $args=array( 'category__in' => $category_ids, 'post__not_in' => array($post->ID), 'ignore_sticky_posts' => 1, 'showposts'=>4,'orderby' => 'rand' ); $my_query = new wp_query( $args ); if( $my_query->have_posts() ) { echo '<div class="related-posts"><h3>'.__('Related Posts','page-builder-framework').'</h3><div class="postauthor-top"><ul>'; $pexcerpt=1; $j = 0; $counter = 0; while( $my_query->have_posts() ) { ++$counter; if($counter == 4) { $postclass = 'last'; $counter = 0; } else { $postclass = ''; } $my_query->the_post();?> <li class="<?php echo $postclass; ?> rpexcerpt<?php echo $pexcerpt ?> <?php echo (++$j % 2 == 0) ? 'last' : ''; ?>"> <a class="relatedthumb" href="<?php the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"> <span class="rthumb"> <?php if(has_post_thumbnail()): ?> <?php the_post_thumbnail('smaller-thumb', 'title='); ?> <?php else: ?> <img src="<?php echo get_template_directory_uri(); ?>/images/no-thumb.jpg" alt="<?php the_title(); ?>" class="wp-post-image" /> <?php endif; ?> </span> <span> <?php the_title(); ?> </span> </a> </li> <?php $pexcerpt++;?> <?php } echo '</ul></div></div>'; }} wp_reset_query(); ?> <!-- End Related Posts -->
Sorry Sir, If I appear like a novice but I don’t know much about proper code functions. I wasted many hours but didn’t find any solution as to how to add related posts after the post. I don’t want to use a plugin as they increase total requests. Could you please help me out?
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘How to add related posts in theme’ is closed to new replies.