2011-12-29 74 views
7

如何在Wordpress中获得随机文章?在Wordpress中获取随机文章

我想在页面上显示一个按钮,当按下它时,它会从博客中随机发布。我不希望在网页上显示随机帖子,我只想要一个导向该帖子的链接。 我试过在谷歌上搜索代码,在这里在stackoverflow但没有成功。

谢谢...

UPDATE:

这里是我的模板代码:

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

或许,这可以帮助你:随机后的插件(http://wordpress.org/extend/plugins/random-posts-plugin/) – Cyclonecode 2011-12-29 19:22:16

+0

谢谢但我希望得到一个简单的代码。这个插件不允许定制链接,因为我可以用一个按钮替换最近的帖子列表。任何其他想法? – rlesko 2011-12-29 19:44:15

+0

@ rlesko a)**不要使用可以通过核心进行全球化的变量 - 不要使用“'$ query'”,“'$ post'”等等。 '$ myQuery'或类似的东西... – 2017-01-31 17:40:12

回答

5

我发现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

感谢大家谁贡献你的帮助...

干杯!

+0

这是一个有趣的解决方案,虽然设计错误 - 你应该宁愿修复原始代码,因为这会带来不必要的重定向;) – 2017-01-31 17:44:09

7

创建一个页面模板,使用下面的代码来获得一个随机的帖子:

//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> 
+1

它似乎没有工作。请检查问题中的更新。 – rlesko 2011-12-30 20:03:40

+0

@rlesko这可能是一个粘滞的帖子问题?您可以尝试将'ignore_sticky_posts'参数设置为**'false' ** :)否则,您的代码中会出现一些重大错误 - 我已对您的问题进行了评论;) – 2017-01-31 17:48:44

+0

不是一个棘手的帖子问题,只是不太明白查询工作。 “兰德”顺序只会改变已查询的帖子的顺序,并告诉它的posts_per_page为1只能带回一个帖子。所以它返回一个帖子,然后随机化订单(又名总是同一个帖子)。 – clark 2017-07-21 17:22:36

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

我发现这是更加有用的将重定向到一个随机的帖子,你可以在侧边栏或菜单作为链接使用的URL。如果它是一个单一的WP网站,甚至在wp.com它真的很容易,一个博客在

http://mygroovywpsite.me/ 

所有你需要做的是将其追加?随机

http://mygroovywpsite.me/?random 

我发现这确实对我的多站点安装中的子站点不起作用(也不是上面的wp_beginner代码),这两种方法都只是加载主页。也许我有一些时髦的缓存问题。我在许多网站上这样做的方式是无插件的几个步骤。

首先要在你的网站被称为“随机” /用塞“随机”页面 - 它并不需要在它的任何内容

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

然后你可以重新指向你博客上的任何链接..... /随机w/o任何摔跤。htaccess的

我已经做到了这种方式,因为我不得不修改查询,有时自定义后的类型,有时限制类等

我只有一个网站,是因为有问题托管抑制使用MySQL查询与ORDER BY RAND()

+0

工程就像魅力。 – 2016-06-13 19:45:06

0

另一种简单的解决方案,以显示随机邮政

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

2.Open页面,删除默认的WP循环和粘贴下面的代码

3.To改变无后的改号“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/