WordPress在一级分类下显示所有二级分类的文章列表

Update:2011-12-9 Comments: 5 Views:832

制作出一个类似于wpbars里面的WordPress插件 ,在Wordpress一级分类下(父分类)显示所有的Wordpress二级分类(子分类)的所有文章列表,而且二级分类下显示所有的文章摘要。这个代码我之前不知道,到wopus问答里面问了一下,prower给出了答案。

<?php 
    global $cat; 
    $cats = get_categories(array( 
        'child_of' => $cat, 
        'parent' => $cat, 
        'hide_empty' => 0 
    )); 
    $c = get_category($cat); 
    if(empty($cats)){ 
?> 
<div class="item"> 
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
    <div class="post"> 
        <h2><a title="<?php the_title(); ?>" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> 
        <p><?php the_excerpt(); ?></p> 
        <p><a href="<?php the_permalink(); ?>">全文阅读>></a></p> 
        <div class="meta"><?php the_time('Y-m-d'); ?> | 标签: <?php the_tags('', ' , ', ''); ?></div> 
    </div> 
    <?php endwhile; ?> 
    <?php else: ?> 
        <div class="post"><p>文章稍后更新</p></div> 
    <?php endif; ?> 
</div> 
<div class="navigation"> 
    <span class="alignleft"><?php next_posts_link('&laquo; Older posts') ?></span> 
    <span class="alignright"><?php previous_posts_link('Newer posts &raquo;') ?></span> 
</div> 
<?php 
}else{ 
    foreach($cats as $the_cat){ 
        $posts = get_posts(array( 
            'category' => $the_cat->cat_ID, 
            'numberposts' => 10, 
        )); 
        if(!empty($posts)){ 
            echo ' 
            <div> 
                <div><h2><a title="'.$the_cat->name.'" href="'.get_category_link($the_cat).'">'.$the_cat->name.'</a></h2></div>   
                <ul>'; 
                    foreach($posts as $post){ 
                        echo '<li><span>'.mysql2date('Y-m-d', $post->post_date).'</span> 
                        <a title="'.$post->post_title.'" href="'.get_permalink($post->ID).'">'.$post->post_title.'</a></li>'; 
                    } 
                echo '</ul> 
            </div>'; 
        } 
    } 

?> 

版权所有,转载请注明出处。
原创:Wordpress主题吧 链接: http://www.wpbars.com/wordpress-show-all-children-category/
编辑: wpbars主题吧 分享到: 转播到腾讯微博

相关搜索

在下列搜索引擎中查找关于“WordPress在一级分类下显示所有二级分类的文章列表”的信息

谷歌搜索百度搜索搜狗搜索搜搜搜索 雅虎搜索有道搜索

已是最新文章

5 个主题帖 其中:热心观众:3 个, 管理员:2 个

  1. citier 沙发

    这个挺好好玩,借用啦

    • wpbars主题吧

      @citier: 嗯,这个可是唯一的一个教程哦。我也是请教别人的。喜欢就拿去。

    • citier

      @wpbars主题吧: 我刚试了一下,我的一级栏目和二级栏目都有内容,没调取到一级栏目的文章

    • wpbars主题吧

      @citier: 你好,一级分类的内容读取不到的,这样子只能调用二级分类的内容而已。我的一级分类下都没有文章的,都是放在二级分类的。

  2. 李超 板凳

    这个代码我看不懂呀,怎么用啊?

/ 快捷键:Ctrl+Enter