2012-01-12 110 views

回答

1
  1. 访问http://example.com/wp-admin/options-permalink.php启用permalinks为你的WordPress安装,如果你想在文章的标题是最后的URL部分,你会选择Custom Structure和将此值设置为%post_name%
  2. 在您当前的主题文件夹中创建一个新页面模板,如下所述:Creating Your Own Page Templates
  3. 创建一个新页面,标题为Random,并在页面编辑屏幕中选择Page Attributes下新创建的页面模板。
  4. 然后在你的页面模板,你会做这样的事情作为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> 
    
2

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

+0

谢谢,我在哪里把这个代码?在我的主题中创建一个新文件?我如何链接到它? – applechief 2012-01-12 10:36:25

+0

您应该制作一个新页面并为此创建一个模板。这里有关于如何做到这一点的文档的链接。 http://codex.wordpress.org/Pages#Templates_by_page-ID_or_page-Slug – Filip 2012-01-12 10:37:44

0

您可以使用此代码添加到网页上显示随机职位。

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

尝试......

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/

相关问题