在 Wordpress 中获取随机帖子

问题描述 投票:0回答:6

如何在 WordPress 中获得随机帖子?

我想在页面上显示一个按钮,按下该按钮后,会转到博客中的随机帖子。我不想在页面上显示随机帖子,我只想要一个指向该帖子的链接。 我尝试在 Google 和 stackoverflow 上搜索代码,但没有成功。

谢谢...

更新:

这是我的模板代码:

<?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(); ?>
wordpress random
6个回答
25
投票

创建页面模板,并使用以下代码获取随机帖子:

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

然后在页面中,只需使用:

<a href="the link to the page">see a random post</a>

7
投票

我发现this帖子给了我想要的结果......

这是从 wpbeginner 博客文章复制/粘贴的解决方案。无意侵犯版权。

只需将以下代码添加到

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;
   }
}

使用

mydomain.com/random/
作为指向随机帖子的按钮的
href

感谢所有为您提供帮助的人...

干杯!


4
投票

我发现拥有一个可以重定向到随机帖子的 URL 更有用,您可以将其用作侧边栏或菜单中的链接。如果它是一个单一的 WP 网站,甚至在 wp.com 上,那真的很容易,博客位于

http://mygroovywpsite.me/

您所需要做的就是在其后面附加 ?random

http://mygroovywpsite.me/?random

我发现这在我的多站点安装中的子站点上不起作用(也没有上面的 wp_beginner 代码),两种方法都只是加载主页。也许我遇到了一些奇怪的缓存问题。我在许多网站上执行此操作的方法是多执行几个步骤,无需插件。

首先在您的网站中创建一个名为“随机”的页面/带有“随机”标签 - 它不需要其中包含任何内容

然后创建一个 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;
}
?>

然后您可以重定向博客上的任何链接...../随机,无需与 .htaccess 进行任何斗争

我这样做是因为我必须修改查询,有时是自定义帖子类型,有时是限制类别等。

我只有一个网站存在问题,因为托管禁止使用 ORDER BY RAND() 的 mySQL 查询


4
投票

显示随机帖子的另一个简单解决方案

1.首先创建一个自定义页面模板。将其命名为随机帖子或您选择的名称!

2.打开页面并删除默认的wp循环并粘贴以下代码

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

来源:http://www.yengkokpam.com/displays-random-posts-in-a-page/


1
投票

检查这个

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

0
投票

只需使用 SQL。

获取结果( ” 选择 * 来自你的表 WHERE post_status = '发布' 按兰德排序() 限制1 ” ); $page_link = $posts[0]->post_name; ?>
© www.soinside.com 2019 - 2024. All rights reserved.