以下是我在的首页使用的一些代码this site. 它从每个类别中抽取一篇帖子(我随机选择了一篇帖子),并显示一些帖子内容。我已经包含了HTML,这样您就可以理解它了。
<div id="main-content" class="main-content-wide">
<?php
//Get the desired categories and order by ID
$cat_args = array(
\'include\' => \'7,8,9,10, 14, 60\',
\'orderby\' => \'ID\',
\'child_of\' => 0
);
//For each category show a random post
$categories = get_categories($cat_args);
foreach($categories as $category)
{
?>
<div class="categorytitle">
<h3>Children\'s Stories <?php echo $category->name; ?></h3>
<a href="<?php echo get_category_link( $category->term_id ); ?>" class="allstories"> (More Stories...)</a>
</div>
<div class="story">
<?php
$post_args = array(
\'numberposts\' => 1,
\'orderby\' => rand,
\'category\' => $category->term_id
);
$posts = get_posts($post_args);
foreach($posts as $post)
{
?>
//Show post content
<?php get_template_part(\'loop\'); ?>
</div>
<?php
}
}
?>
</div>