Status
Not open for further replies.

ntn0408

Active Member
58
2014
12
80
Hi everybody,
I'm using Wordpress last version is 3.9.1.
And my theme using Relate Posts Thumbnails without plugin. But it only show when same tag (ex: 2 posts have same 1 tag). Please help me edit the function to show Related Post Thumbnails via Category.

Code in function.php below:
function get_related_posts($post_id, $tags = array()) {
$query = new WP_Query();

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

$args = wp_parse_args($args, array(
'showposts' => 3,
'post__not_in' => array($post_id),
'tag__in' => $tagsA,
'ignore_sticky_posts' => 1,
));

$query = new WP_Query($args);

return $query;
}


Code in single.php below:
<div class="post-box-wrapper">

<div class="post-box">

<h5>Related Posts</h5>

<?php while($related->have_posts()): $related->the_post(); ?>
<?php if($count == 4): $count = 1; endif; if($count == 3): $class = 'last'; else: $class = ''; endif; ?>
<?php if(has_post_thumbnail()): ?>
<?php $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'related-posts-image'); ?>
<div class="related-item <?php echo $class; ?>">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><img src="<?php echo $image[0]; ?>" alt="<?php the_title(); ?>" /></a>
</div>
<?php endif; ?>
<?php $count++; endwhile; ?>

</div>

</div>


Pro wordpress code please help me edit.
Thanks so much.
 
2 comments
function get_related_posts($post_id, $tags = array()) {
$query = new WP_Query();

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

$args = wp_parse_args($args, array(
'showposts' => 3,
'post__not_in' => array($post_id),
'tag__in' => $tagsA,
'ignore_sticky_posts' => 1,
));

$query = new WP_Query($args);

return $query;
}

Part of the solution would be to replace 'tag__in' with 'category__in' but you also need to find the code where get_related_posts() is being called and switch out the tags array argument with a categories array.

That code might be in your single.php file somewhere above the code you posted. It would possibly look something like this:
Code:
<?php $tags = [B]get_the_tags()[/B]; ?>
<?php if($tags): ?>
<?php $related = [B]get_related_posts($post->ID, $tags)[/B]; ?>

In that case, you would replace the get_the_tags() function with get_the_category().

You might also want to rename the $tags variables to $categories just so the code makes more sense, but that's optional.

Hope that helps!
 
Last edited:
function get_related_posts($post_id, $tags = array()) {
$query = new WP_Query();

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

$args = wp_parse_args($args, array(
'showposts' => 3,
'post__not_in' => array($post_id),
'tag__in' => $tagsA,
'ignore_sticky_posts' => 1,
));

$query = new WP_Query($args);

return $query;
}

Part of the solution would be to replace 'tag__in' with 'category__in' but you also need to find the code where get_related_posts() is being called and switch out the tags array argument with a categories array.

That code might be in your single.php file somewhere above the code you posted. It would possibly look something like this:
Code:
<?php $tags = [B]get_the_tags()[/B]; ?>
<?php if($tags): ?>
<?php $related = [B]get_related_posts($post->ID, $tags)[/B]; ?>

In that case, you would replace the get_the_tags() function with get_the_category().

You might also want to rename the $tags variables to $categories just so the code makes more sense, but that's optional.

Hope that helps!
Thanks allroy for help! That is useful infomation :D
 
Status
Not open for further replies.
Back
Top