Community Forums › Forums › Archived Forums › Design Tips and Tricks › Related Posts with Thumbnails
Tagged: related posts
- This topic has 1 reply, 2 voices, and was last updated 7 years, 11 months ago by Brad Dalton.
-
AuthorPosts
-
May 12, 2016 at 2:11 pm #185354vangelisbelMember
Hello, I've been using the following script for related posts with thumbnails. It used to work fine, but ever since I changed the number of thumbnails for 5 to 3, I only get the same 3 results in every post in a specific category.
E.g. In the category for Santorini weddings http://www.vangelisphotography.com/santorini-wedding/ every post displays the same 3 related posts.
Does anybody have an idea if there is a problem with the script below:
/** Display related posts in Genesis based on Category */
//for HTML5 themes
add_action( 'genesis_after_entry_content', 'child_related_posts' );
/**
* Outputs related posts with thumbnail
*/
function child_related_posts() {if ( is_single ( ) ) {
global $post;
$count = 0;
$postIDs = array( $post->ID );
$related = '';
$tags = wp_get_post_tags( $post->ID );
$cats = wp_get_post_categories( $post->ID );if ( $tags ) {
foreach ( $tags as $tag ) {
$tagID[] = $tag->term_id;
}
$args = array(
'tag__in' => $tagID,
'post__not_in' => $postIDs,
'showposts' => 3,
'ignore_sticky_posts' => 1,
'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'
)
)
);$tag_query = new WP_Query( $args );
if ( $tag_query->have_posts() ) {
while ( $tag_query->have_posts() ) {
$tag_query->the_post();
$img = genesis_get_image() ? genesis_get_image( array( 'size' => 'related' ) ) : '';
$related .= '
- ' . $img . get_the_title() . '
';
$postIDs[] = $post->ID;
$count++;
}
}
}if ( $count <= 4 ) {
$catIDs = array( );
foreach ( $cats as $cat ) {
if ( 3 == $cat )
continue;
$catIDs[] = $cat;}
$showposts = 3 - $count;
$args = array(
'category__in' => $catIDs,
'post__not_in' => $postIDs,
'showposts' => $showposts,
'ignore_sticky_posts' => 1,
'orderby' => 'rand',
'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'
)
)
);$cat_query = new WP_Query( $args );
if ( $cat_query->have_posts() ) {
while ( $cat_query->have_posts() ) {
$cat_query->the_post();
$img = genesis_get_image() ? genesis_get_image( array( 'size' => 'related' ) ) : '';
$related .= '
- ' . $img . get_the_title() . '
';
}
}
}if ( $related ) {
printf( '<div class="related-posts"><h3 class="related-title">You May Also Like</h3><ul class="related-list">%s</div>', $related );
}
wp_reset_query();
}
}May 13, 2016 at 3:58 am #185554Brad DaltonParticipant -
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.