Bài đăng liên quan với hình thu nhỏ trong Genesis - Đã tải lại

Đoạn mã dưới đây khi được thêm vào functions.php của chủ đề con sẽ in ra 5 bài đăng liên quan đến mục nhập hiện tại bên trên các nhận xét về các bài đăng đơn lẻ trong Genesis.

add_action( 'genesis_before_comments', 'sk_related_posts', 12 );
/**
* Outputs related posts with thumbnail
*
* @author Nick the Geek
* @url http://designsbynickthegeek.com/tutorials/related-posts-genesis
* @global object $post
*/
function sk_related_posts() {

global $do_not_duplicate;

if ( ! is_singular ( 'post' ) ) {
return;
}

$count = 0;

$related = '';

$do_not_duplicate = array();

$tags = wp_get_post_tags( get_the_ID() );

$cats = wp_get_post_categories( get_the_ID() );

// If we have some tags, run the tag query.
if ( $tags ) {
$query = sk_related_tag_query( $tags, $count );
$related .= $query['related'];
$count = $query['count'];
}

// If we have some categories and less than 5 posts, run the cat query.
if ( $cats && $count <= 4 ) {
$query = sk_related_cat_query( $cats, $count );
$related .= $query['related'];
$count = $query['count'];
}

// End here if we don't have any related posts.
if ( ! $related ) {
return;
}

// Display the related posts section.
echo '<div class="related">';
echo '<h3 class="related-title">You might also enjoy...</h3>';
echo '<div class="related-posts-list" data-columns>' . $related . '</div>';
echo '</div>';

}

function sk_related_tag_query( $tags, $count ) {

global $do_not_duplicate;

if ( ! $tags ) {
return;
}

$postIDs = array( get_the_ID() );

foreach ( $tags as $tag ) {
$tagID[] = $tag->term_id;
}

$tax_query = array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array(
'post-format-link',
'post-format-status',
'post-format-aside',
'post-format-quote'
),
'operator' => 'NOT IN'
)
);
$args = array(
'tag__in' => $tagID,
'post__not_in' => $postIDs,
'showposts' => 5,
'ignore_sticky_posts' => 1,
'tax_query' => $tax_query,
);

$related = '';

$tag_query = new WP_Query( $args );

if ( $tag_query->have_posts() ) {
while ( $tag_query->have_posts() ) {
$tag_query->the_post();

$do_not_duplicate[] = get_the_ID();

$count++;

// $title = genesis_truncate_phrase( get_the_title(), 35 );
$title = get_the_title();

$related .= '<div class="related-post">';
$related .= '<a class="related-post-title" href="' . get_permalink() . '" rel="bookmark" title="Permanent Link to ' . $title . '">' . $title . '</a>';
$related .= '<a class="related-image" href="' . get_permalink() . '" rel="bookmark" title="Permanent Link to ' . $title . '">' . genesis_get_image( array( 'size' => 'related' ) ) . '</a>';
$related .= '</div>';
}
}

wp_reset_postdata();

$output = array(
'related' => $related,
'count' => $count
);

return $output;
}

function sk_related_cat_query( $cats, $count ) {

global $do_not_duplicate;

if ( ! $cats ) {
return;
}

$postIDs = array_merge( array( get_the_ID() ), $do_not_duplicate );

$catIDs = array();

foreach ( $cats as $cat ) {
if ( 3 == $cat ) {
continue;
}
$catIDs[] = $cat;
}

$showposts = 5 - $count;

$tax_query = array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array(
'post-format-link',
'post-format-status',
'post-format-aside',
'post-format-quote'
),
'operator' => 'NOT IN'
)
);
$args = array(
'category__in' => $catIDs,
'post__not_in' => $postIDs,
'showposts' => $showposts,
'ignore_sticky_posts' => 1,
'orderby' => 'rand',
'tax_query' => $tax_query,
);

$related = '';

$cat_query = new WP_Query( $args );

if ( $cat_query->have_posts() ) {
while ( $cat_query->have_posts() ) {
$cat_query->the_post();

$count++;

// $title = genesis_truncate_phrase( get_the_title(), 35 );
$title = get_the_title();

$related .= '<div class="related-post">';
$related .= '<a class="related-post-title" href="' . get_permalink() . '" rel="bookmark" title="Permanent Link to ' . $title . '">' . $title . '</a>';
$related .= '<a class="related-image" href="' . get_permalink() . '" rel="bookmark" title="Permanent Link to ' . $title . '">' . genesis_get_image( array( 'size' => 'related' ) ) . '</a>';
$related .= '</div>';

}
}

wp_reset_postdata();

$output = array(
'related' => $related,
'count' => $count
);

return $output;

}