列出wordpress中的所有类别和所有帖子

时间:2016-10-12 15:05:33

标签: wordpress

如何列出wordpress中的所有类别和所有帖子,如下例所示:

CATEGORY1
POSTS

CATEGORY2
POSTS

CATEGORY3
POSTS

2 个答案:

答案 0 :(得分:1)

尝试以下代码:

  <?php // get all the categories from the database
$cats = get_categories();
// loop through the categries
foreach ($cats as $cat) {
// setup the cateogory ID
$cat_id= $cat->term_id;
// Make a header for the cateogry
echo “<h2>”.$cat->name.”</h2>”;
// create a custom wordpress query
query_posts(“cat=$cat_id&post_per_page=100″);

if (have_posts()) : while (have_posts()) : the_post(); ?>

<?php // create our link now that the post is setup ?>

<a href=”<?php the_permalink();?>”><?php the_title(); ?></a>

<?php echo ‘<hr/>’; ?>

<?php endwhile; endif;
// done our wordpress loop. Will start again for each category ?>

<?php } // done the foreach statement ?>

答案 1 :(得分:0)

我要做的就是调用新的WP查询3次,无限数量的帖子如下:

<?php
    $myposts = get_posts('numberposts=-1&category=CATNUMBER');
    foreach($myposts as $post) : setup_postdata($post);
?>
<div class="eachPost">
    <div class="eachImage">
        <?php the_post_thumbnail('full'); ?>
    </div>
    <h2><?php the_title(); ?></h2>
    <?php the_content(); ?>
 </div>
 <?php endforeach;?>

使用类别ID更改CATNUMBER并重复3次。希望这会有所帮助。