array($post->ID), 'showposts'=> 8, 'orderby' => 'date', 'order' => 'DESC' ); //logic for blog posts if (is_singular('post')) { //related text $related_text = esc_html__("Related Articles", "kleo_framework"); $categories = get_the_category($post->ID); if (!empty($categories)) { $category_ids = array(); foreach ($categories as $rcat) { $category_ids[] = $rcat->term_id; } $args['category__in'] = $category_ids; } } // logic for custom post types else { //related text $related_text = esc_html__("Related", "kleo_framework"); global $post; $categories = get_object_taxonomies($post); if (!empty($categories)) { foreach( $categories as $tax ) { $terms = wp_get_object_terms($post->ID, $tax, array('fields' => 'ids')); $args['tax_query'][] = array( 'taxonomy' => $tax, 'field' => 'id', 'terms' => $terms ); } } } /* Remove this line to show related posts even no categories are found */ if (!$categories) { return; } ?>