0 ){ $rel_count = 0; $rel_posts = array(); $tag_ids = array(); if ( has_tag() ) { $tags = wp_get_post_tags($post->ID); foreach($tags as $tag): array_push( $tag_ids, $tag -> term_id); endforeach ; $tag_args = array( 'post__not_in' => array($post -> ID), 'posts_per_page'=> $max_post_num, 'tag__in' => $tag_ids, 'orderby' => 'rand', ); $rel_posts = get_posts($tag_args); $rel_count = count($rel_posts); } if(!has_tag() || $max_post_num > $rel_count){ $categories = get_the_category($post->ID); $category_ID = array(); foreach($categories as $category): array_push( $category_ID, $category -> cat_ID); endforeach ; $cat_args = array( 'post__not_in' => array($post -> ID), 'tag__not_in' => $tag_ids, 'posts_per_page'=> ($max_post_num - $rel_count), 'category__in' => $category_ID, 'orderby' => 'rand', ); $cat_posts = get_posts($cat_args); $rel_posts = array_merge($rel_posts, $cat_posts); shuffle($rel_posts); } if( count( $rel_posts ) > 0 ): ?>