最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

customization - How to get different Related Posts

programmeradmin1浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 1

Randomizing 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.

发布评论

评论列表(0)

  1. 暂无评论