admin管理员组文章数量:1414900
For some reason most of our blog pages show the same or very similar related posts Im using this code:
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
shuffle($tag_ids);
$args=array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=>4, // Number of related posts to display.
'caller_get_posts'=>1,
'post_type' =>'therapyspark'
);
$my_query = new wp_query( $args );
while( $my_query->have_posts() ) {
$my_query->the_post();
?>
<div class="relateddiv"><div class = "relatedimg">
<a rel="external" class = "relatedlink" href="<?php the_permalink()?>"><?php the_post_thumbnail(array(150,100)); ?><br />
</div><div class = "relatedtitle"><?php the_title(); ?></div>
</a>
</div>
<?php}
}
$post = $orig_post;
wp_reset_query();
?>
The shuffle doesnt seem to make a difference. What could I do to show different related posts
For some reason most of our blog pages show the same or very similar related posts Im using this code:
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
shuffle($tag_ids);
$args=array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=>4, // Number of related posts to display.
'caller_get_posts'=>1,
'post_type' =>'therapyspark'
);
$my_query = new wp_query( $args );
while( $my_query->have_posts() ) {
$my_query->the_post();
?>
<div class="relateddiv"><div class = "relatedimg">
<a rel="external" class = "relatedlink" href="<?php the_permalink()?>"><?php the_post_thumbnail(array(150,100)); ?><br />
</div><div class = "relatedtitle"><?php the_title(); ?></div>
</a>
</div>
<?php}
}
$post = $orig_post;
wp_reset_query();
?>
The shuffle doesnt seem to make a difference. What could I do to show different related posts
Share Improve this question asked Sep 16, 2019 at 14:01 user718229user718229 1033 bronze badges 2- Are you using any WP_Cache, or other site caching method (W3 Total Cache, etc)? – Mike Baxter Commented Sep 16, 2019 at 17:01
- No. they arent all the same...Id just like have it suggest different related posts, like if you refreshed the page it would suggest others maybe – user718229 Commented Sep 16, 2019 at 18:05
1 Answer
Reset to default 1Randomizing your tags doesn't matter much to WP_Query. If you want random results, try adding 'orderby' => 'rand'
to your $args. This should randomize the stories returned. If not, try applying shuffle()
to $my_query
before iterating through them. Some web hosts have reportedly disabled the 'orderby'=>'rand'
method of querying posts. For these hosts shuffle()
seems to still get the desired results.
NOTE: If you go with the shuffle()
method, you probably want to replace the posts_per_page value with '-1' and add a loop counter to your output loop. Otherwise, you will always be shuffling the first four stories.
本文标签: customizationHow to get different Related Posts
版权声明:本文标题:customization - How to get different Related Posts 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745153317a2645017.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论