|
<?php |
|
// Do NOT include opening tag |
|
|
|
/** |
|
* Related Popular Posts. |
|
* |
|
* @package StudioPlayer |
|
* @link https://rainastudio.com/themes/studioplayer |
|
* @author RainaStudio |
|
* @copyright Copyright © 2018 RainaStudio |
|
* @license GPL-2.0+ |
|
*/ |
|
|
|
function studio_player_related_posts_setup($args = array()) { |
|
if ( is_singular('post') ) { |
|
?> |
|
<div class="related-posts"> |
|
<h4 class="widget-title">Related Popular Posts</h4><?php |
|
|
|
global $post; |
|
|
|
// Default args |
|
$args = wp_parse_args($args, array( |
|
'post_id' => !empty($post) ? $post->ID : '', // Exclude current post |
|
'taxonomy' => 'category', // Show posts by category |
|
'limit' => 3, // How many items to display |
|
'post_type' => !empty($post) ? $post->post_type : 'post', |
|
'orderby' => 'rand', // Randomize the results |
|
'order' => 'DESC' |
|
)); |
|
|
|
// Check taxonomy |
|
if (!taxonomy_exists($args['taxonomy'])) { |
|
return; |
|
} |
|
|
|
// Post taxonomies |
|
$taxonomies = wp_get_post_terms($args['post_id'], $args['taxonomy'], array('fields' => 'ids')); |
|
|
|
if (empty($taxonomies)) { |
|
return; |
|
} |
|
|
|
// Custom post query |
|
$related_posts = get_posts(array( |
|
'post__not_in' => (array) $args['post_id'], |
|
'post_type' => $args['post_type'], |
|
'tax_query' => array( |
|
array( |
|
'taxonomy' => $args['taxonomy'], |
|
'field' => 'term_id', |
|
'terms' => $taxonomies |
|
), |
|
), |
|
'posts_per_page' => $args['limit'], |
|
'orderby' => $args['orderby'], |
|
'order' => $args['order'] |
|
)); |
|
|
|
// Layout the loop |
|
if (!empty($related_posts)) { ?> |
|
<div class="related-posts-wrap"> |
|
<ul class="related-posts-list"> |
|
<?php |
|
foreach ($related_posts as $post) { |
|
setup_postdata($post); |
|
?> |
|
<li> |
|
<a class="title" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> |
|
<?php if (has_post_thumbnail()) { ?> |
|
<div class="thumb"> |
|
<?php echo get_the_post_thumbnail(null, 'medium', array('alt' => the_title_attribute(array('echo' => false)))); ?> |
|
</div> |
|
<?php } ?> |
|
<p class="title"><?php the_title(); ?></p> |
|
</a> |
|
</li> |
|
<?php } ?> |
|
</ul> |
|
<div class="clearfix"></div> |
|
</div> |
|
<?php |
|
} |
|
wp_reset_postdata(); |
|
?></div><?php |
|
|
|
} |
|
} |
|
|
|
add_action( 'genesis_after_entry', 'studio_player_related_posts_setup', 5 ); |