WordPress的:随机文章的页面
问题描述:
我还没有多与wordpress工作,我想要做的是有一个页面显示随机的职位。WordPress的:随机文章的页面
这与我的主页显示最新帖子相似,但每次页面刷新时都会显示随机帖子。 如果主页在http://example.com我想我的随机页面在http://example.com/random
如何做到这一点?
答
- 访问
http://example.com/wp-admin/options-permalink.php
启用permalinks
为你的WordPress安装,如果你想在文章的标题是最后的URL部分,你会选择Custom Structure
和将此值设置为%post_name%
- 在您当前的主题文件夹中创建一个新页面模板,如下所述:Creating Your Own Page Templates
- 创建一个新页面,标题为
Random
,并在页面编辑屏幕中选择Page Attributes
下新创建的页面模板。 -
然后在你的页面模板,你会做这样的事情作为Filip建议:
<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>
答
参数get_posts
接受值rand
。在文档中
<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>
更多信息:http://codex.wordpress.org/Template_Tags/get_posts#Random_posts
答
您可以使用此代码添加到网页上显示随机职位。
<?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; ?>
答
尝试......
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/
谢谢,我在哪里把这个代码?在我的主题中创建一个新文件?我如何链接到它? – applechief 2012-01-12 10:36:25
您应该制作一个新页面并为此创建一个模板。这里有关于如何做到这一点的文档的链接。 http://codex.wordpress.org/Pages#Templates_by_page-ID_or_page-Slug – Filip 2012-01-12 10:37:44