• Resolved Jivansutra

    (@ppsren)


    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)
  • Theme Author David Vongries

    (@davidvongries)

    Hi @ppsren,

    Please understand that we can’t provide support for custom development.

    Instead of tweaking the single.php file, I’d suggest to use a hook to output the code on the actual post like this:

    function related_posts() {
    
    	global $post;
    
    	$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();
    
    }
    add_action( 'wpbf_after_article', 'related_posts' );

    Keep in mind that this is just a starting point and that I’ve simply used the code you’ve provided which can be optimized & cleaned up a lot.

    The code above would be placed in the child-themes functions.php file.

    Best,
    David

    Thread Starter Jivansutra

    (@ppsren)

    Hi sir, thanks for your reply. code worked but instead of showing thumbnails it is showing the full image and that is also after the comment box. Not helpful. Will upgrading to PRO theme give this functionality?

    Theme Author David Vongries

    (@davidvongries)

    Hi @ppsren,

    As I mentioned, this is really just a starting point and requires more coding/design work.

    I’d suggest to outsource custom development to a service like Upwork for instance.

    There should also be plugins available to display related posts but I haven’t used any myself and can’t make suggestions here.

    Best,
    David

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to add related posts in theme’ is closed to new replies.