php - How to show blog posts changing every time after refreshing the page in non-wordpress website?

I have made a page in non-wordpress website which is showing the post from blog of wordpress website, it is working prop

I have made a page in non-wordpress website which is showing the post from blog of wordpress website, it is working properly. I have added some PHP code in my page. Now I want that every time I refresh the page it will show different different post every time.

<?php 
  define('WP_USE_THEMES', false);
  require('blog/wp-load.php');
  query_posts('showposts=4');
?>

<ul style="margin-top:0px">

<?php while (have_posts()): the_post(); ?>
 <li style="border: 1px solid #edf4fc;margin-bottom:30px">
<div class="img-hover-zoom">
  <a target="_blank" href="<?php the_permalink(); ?>"><p>
  <?php
    if ( has_post_thumbnail() ) {
      the_post_thumbnail();
    }
  ?>
 </p>
 <div class="content">
 <h4 style="font-size:18px;font-weight:500;"><?php the_title(); ?></h4>

</div>
</a></div>
 </li>

<?php endwhile; ?>
</ul>```

It is showing 5 post but I want to show 5 random post.

I have made a page in non-wordpress website which is showing the post from blog of wordpress website, it is working properly. I have added some PHP code in my page. Now I want that every time I refresh the page it will show different different post every time.

<?php 
  define('WP_USE_THEMES', false);
  require('blog/wp-load.php');
  query_posts('showposts=4');
?>

<ul style="margin-top:0px">

<?php while (have_posts()): the_post(); ?>
 <li style="border: 1px solid #edf4fc;margin-bottom:30px">
<div class="img-hover-zoom">
  <a target="_blank" href="<?php the_permalink(); ?>"><p>
  <?php
    if ( has_post_thumbnail() ) {
      the_post_thumbnail();
    }
  ?>
 </p>
 <div class="content">
 <h4 style="font-size:18px;font-weight:500;"><?php the_title(); ?></h4>

</div>
</a></div>
 </li>

<?php endwhile; ?>
</ul>```

It is showing 5 post but I want to show 5 random post.
Share Improve this question asked Jul 1, 2019 at 6:08 Shobha PatwalShobha Patwal 13 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 1

This line is responsible for posts that are showing:

query_posts('showposts=4');

All you tell in it is “take 4 posts” and WP will take 4 most recent posts, because that’s the default behavior.

If you want to take random posts, you have to tell WP to order posts randomly:

query_posts( array(
    'posts_per_page' => 4,
    'orderby' => 'rand'
) );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745355073a4624065.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信