madeinthearcade
2/22/2017 - 11:20 PM

Show related posts within category but exclude current post

Show related posts within category but exclude current post

<?php
$args = array(
'numberposts' => 5,
'offset' => 0,
'category' => 3,
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' =>'',
'post_type' => 'post',
'post_status' => 'draft, publish, future, pending, private',
'post__not_in' => array( $post->ID ),
'suppress_filters' => true
);
$recent_posts = wp_get_recent_posts( $args );
echo '<ul class="recent-posts">';
foreach( $recent_posts as $recent ){
echo '<li><span class="date"><i class="fa fa-clock-o" aria-hidden="true"></i> ' . mysql2date( get_option('date_format'), $recent["post_date"] ) . '</span>';
echo '<a href="' . get_permalink($recent["ID"]) . '">' . $recent["post_title"] . '<span class="article-button"><i class="fa fa-arrow-up" aria-hidden="true"></i> Read more</span></a></li>';
}
echo '</ul>';
wp_reset_query();
?>
<h2><?php _e( 'Related stories in: ', 'html5blank' ); the_category(', '); ?></h2>

	<?php
	$related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'numberposts' => 5, 'post__not_in' => array($post->ID) ) );
	if( $related ) foreach( $related as $post ) {
		setup_postdata($post); ?>
		<ul> 
			<li>
				<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
				<?php the_content('Read the rest of this entry &raquo;'); ?>
			</li>
		</ul>   
		<?php }
		wp_reset_postdata(); ?>
<?php
/*
This is a custom made template for the post category: Documents. Here will it display
all posts within subcategories and post them on the parent category page.
*/
?>

<?php get_header(); ?>
<div id="content">
	<div class="container">
		<?php
		the_archive_title( '<h1 class="page-title">', '</h1>' );
		the_archive_description( '<div class="taxonomy-description">', '</div>' );
		?>
		<?php $parentCatName = single_cat_title('',false); $parentCatID = get_cat_ID($parentCatName); $childCats = get_categories( 'child_of='.$parentCatID );
			if(is_array($childCats)):
			foreach($childCats as $child){ ?>
				<div class="box grey">
					<div class="box-info">
						<h3 class="sub-category-title"><?php echo $child->name; ?></h3>
						<?php query_posts('cat='.$child->term_id);
						while(have_posts()): the_post(); $do_not_duplicate = $post->ID; ?>
							<div class="downloads">
								<h5 class="h2 entry-title"><a href="<?php the_field('pdf_downloads'); ?>" download><?php the_title(); ?></a></h5>
								<em>(PDF Size | <?php the_field('file_size'); ?>kb )</em>
								<a href="<?php the_field('pdf_downloads'); ?>" download>Download File</a>
								<section class="entry-content cf">
									<?php the_post_thumbnail( 'bones-thumb-300' ); ?>
									<?php the_excerpt(); ?>
								</section>
							</div>
						<?php endwhile; wp_reset_query(); ?>
						<!-- /box-info -->
					</div>
					<!-- /product-downloads -->
				</div>
			<?php } ?>			
		<?php endif; ?>
	</div>
</div>
<?php get_footer(); ?>