如何显示随机帖子类型和类别的WordPress [英] How to show random post type and category wordpress

查看:97
本文介绍了如何显示随机帖子类型和类别的WordPress的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段代码显示特定类别的随机帖子类型,它显示一个帖子,但现在确实显示随机帖子,我希望用户每次刷新此页面时都显示一个随机帖子,任何建议和建议将不胜感激.

I have a piece of code to show a random post type of specific category, it show one post but it does now show random, I wish to show a random post every single time the user refresh this page, any advice and suggestions will be greatly ppreciated.

Thank:)

    <?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(); ?>

推荐答案

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个帖子的列表

Here it displays a list of 5 posts selected randomly by using the MySQL RAND() function for the orderby parameter value

这篇关于如何显示随机帖子类型和类别的WordPress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆