在 Wordpress 中获取随机帖子
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8672401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get random post in Wordpress
提问by rlab
How do I get a random post in Wordpress?
如何在 Wordpress 中获得随机帖子?
I would like to display a button on a page that, when pressed, goes to a random post from the blog. I don't want a random post to be displayed on the page, I just want a link that leads to that post. I tried searching for a code on Google and here at stackoverflow but no success.
我想在页面上显示一个按钮,当按下该按钮时,会转到博客中的随机帖子。我不想在页面上显示随机帖子,我只想要一个指向该帖子的链接。我尝试在 Google 和 stackoverflow 上搜索代码,但没有成功。
Thanks...
谢谢...
UPDATE:
更新:
Here is my template code:
这是我的模板代码:
<?php /*Template Name: Random*/ ?>
<?php get_header(); ?>
<nav><?php wp_nav_menu(array('menu' => 'Main Nav Menu')); ?></nav>
<div id="main-content-archive">
<div class="grey-text">Random post</div>
<?php $query = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => '1' ) );?>
<?php if (have_posts()) : while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<li>';
the_title();
echo '</li>';
?>
<?php endwhile; ?>
<?php else : ?>
<h2>Not Found</h2>
<?php endif; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
采纳答案by rlab
I found thispost which gave me desired results...
我发现这篇文章给了我想要的结果......
Here's a solution copy/pasted from the wpbeginner blog post. No copyright infringement intended.
这是从 wpbeginner 博客文章复制/粘贴的解决方案。无侵犯版权之意。
Just add the following code to the functions.php
file:
只需将以下代码添加到functions.php
文件中:
add_action('init','random_add_rewrite');
function random_add_rewrite() {
global $wp;
$wp->add_query_var('random');
add_rewrite_rule('random/?$', 'index.php?random=1', 'top');
}
add_action('template_redirect','random_template');
function random_template() {
if (get_query_var('random') == 1) {
$posts = get_posts('post_type=post&orderby=rand&numberposts=1');
foreach($posts as $post) {
$link = get_permalink($post);
}
wp_redirect($link,307);
exit;
}
}
Use mydomain.com/random/
as your href
for your button that leads to the random post.
使用mydomain.com/random/
作为您href
为您的按钮,导致随机职位。
Thanks everyone who contributed for your help...
感谢所有为您的帮助做出贡献的人...
Cheers!
干杯!
回答by bingjie2680
create a page template, and use the following code to get a random post:
创建一个页面模板,并使用以下代码获取随机帖子:
//Create WordPress Query with 'orderby' set to 'rand' (Random)
$the_query = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => '1' ) );
// output the random post
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Post Data
wp_reset_postdata();
then in a page, just use:
然后在页面中,只需使用:
<a href="the link to the page">see a random post</a>
回答by cogdog
I find it is more useful to have a URL that will redirect to a random post that you can use as link in sidebar or in menus. If it is a single WP site and even on wp.com it's really easy, for a blog at
我发现有一个 URL 会重定向到一个随机帖子,您可以将其用作侧边栏或菜单中的链接,这会更有用。如果它是一个单一的 WP 站点,甚至在 wp.com 上,这真的很容易,对于博客
http://mygroovywpsite.me/
All you need to do is append it with ?random
你需要做的就是用 ?random 附加它
http://mygroovywpsite.me/?random
I found this did not work (nor the wp_beginner code above) on subsites in my multisite installation, either approach just loaded the home page. Maybe I had some funky cache issues. The way I do this on many sites is a few more steps w/o plugins.
我发现这在我的多站点安装中的子站点上不起作用(也不是上面的 wp_beginner 代码),这两种方法都只是加载了主页。也许我有一些时髦的缓存问题。我在许多网站上执行此操作的方式是多一些步骤,无需插件。
First make a Page in your site called "Random" / with the slug "random" -- it does not need any content in it
首先在您的站点中创建一个名为“Random”的页面/带有“random”的slug——它不需要任何内容
Then create a page-random.php template
然后创建一个 page-random.php 模板
<?php
/*
Random Post Picker
Use on page to send viewer to random post optionally mod query
*/
// set arguments for WP_Query on published posts to get 1 at random
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 1,
'orderby' => 'rand'
);
// It's time! Go someplace random
$my_random_post = new WP_Query ( $args );
while ( $my_random_post->have_posts () ) {
$my_random_post->the_post ();
// redirect to the random post
wp_redirect ( get_permalink () );
exit;
}
?>
Then you get the re-direct for any link on your blog ...../random w/o any wrestling with .htaccess
然后,您将获得博客上任何链接的重定向...../random w/o 与 .htaccess 的任何角力
I've done it this way because I've had to modify the query, sometimes for custom post type, sometimes to restrict to category, etc.
我这样做是因为我不得不修改查询,有时是自定义帖子类型,有时是限制类别等。
I only had one site that was a problem because the hosting suppressed the use of mySQL queries with ORDER BY RAND()
我只有一个站点有问题,因为托管通过 ORDER BY RAND() 抑制了 mySQL 查询的使用
回答by pradip
Another Simple solution to display Random Post
显示随机帖子的另一个简单解决方案
1.First a create a custom page template. Name it as random post or a name of your choice!
1.首先创建一个自定义页面模板。将其命名为随机帖子或您选择的名称!
2.Open the page and remove the default wp loop and Paste the code below
2.打开页面,去掉默认的wp循环,粘贴下面的代码
3.To change the no of post change the number ‘1' to your choice!
3.要更改帖子编号,请将数字“1”更改为您的选择!
<?php query_posts(array('orderby' => 'rand', 'showposts' => 1)); if (have_posts()) : while (have_posts()) : the_post(); ?> <h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1> <?php the_content(); ?> <?php endwhile; endif; ?>
<?php query_posts(array('orderby' => 'rand', 'showposts' => 1)); if (have_posts()) : while (have_posts()) : the_post(); ?> <h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1> <?php the_content(); ?> <?php endwhile; endif; ?>
source: http://www.yengkokpam.com/displays-random-posts-in-a-page/
来源:http: //www.yengkokpam.com/displays-random-posts-in-a-page/
回答by Dishan TD
Check This
检查这个
<ul>
<?php
$args = array( 'numberposts' => 5, 'orderby' => 'rand' );
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>