如何显示随机的帖子类型和类别wordpress

时间:2015-01-22 11:26:08

标签: wordpress post categories

我有一段代码可以显示特定类别的随机帖子类型,它显示一个帖子但它现在显示随机,我希望每次用户刷新此页面时显示随机帖子,任何建议和建议会大大贬低。

Thansk:)

    <?php

global $post;
$args = array(
    'post_type'=>'topics',
    'showposts'=>'1',
    'cat'=> 8,
    'orderby' => 'rand'
);

$query = 'orderby=rand';
$my_query = new WP_Query($args);?>
<?php 
while ($my_query->have_posts()) : $my_query->the_post();?>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>

<?php endwhile; 
wp_reset_query(); ?>

1 个答案:

答案 0 :(得分:3)

Hi Use this code for getting random posts..It works for me !!!

<h1>Random Posts</h1>
<ul>
 <?php
$args = array( 'posts_per_page' => 5, 'orderby' => 'rand','category' =>'8','post_type' => 'topics' );
$rand_posts = get_posts( $args );
foreach ( $rand_posts as $post ) : 
  setup_postdata( $post ); ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; 
wp_reset_postdata(); ?>
</ul>

此处显示了使用MySQL RAND()函数为orderby参数值随机选择的5个帖子的列表