最新活动:买一送一!升级会员,最高返 500 抵扣券!>>>

WordPress按分类显示文章

WordPress 可以自动将Wordpress站点的文章按分类显示在一个页面中,用作CMS主题布局很实用。 用下代码替换首页模板主循环,则按分类显示各分类最新的5篇文章。 <?php global $cat; $cats = get_categories(array( 'child_of' => $cat...
WordPress按分类显示文章
WordPress

可以自动将Wordpress站点的文章按分类显示在一个页面中,用作CMS主题布局很实用。

用下代码替换首页模板主循环,则按分类显示各分类最新的5篇文章。

<?php
    global $cat;
    $cats = get_categories(array(
        'child_of' => $cat,
        'parent' => $cat,
        'hide_empty' => 0
    ));
    $c = get_category($cat);
    foreach($cats as $the_cat){
        $posts = get_posts(array(
            'category' => $the_cat->cat_ID,
            'numberposts' => 5,
        ));
        if(!empty($posts)){
            echo '
            <div class="item cat_item">
                <div class="item_title"><h2><a title="'.$the_cat->name.'" href="'.get_category_link($the_cat).'">'.$the_cat->name.'</a></h2></div>
                <ul class="box_list">';
                    foreach($posts as $post){
                        echo '<li><span class="alignright">'.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>';
        }
    }
?>

用下代码替换分类归档模板主循环,则显示该分类下所有子分类的最新5篇文章,并按分类显示。如果当前分类下不存在子分类,则显示该分类的文章列表!

<?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%20the_permalink();%20?>"><?php the_title(); ?></a></h2>
        <p><?php the_excerpt(); ?></p>
        <p><a href="<?php%20the_permalink();%20?>">全文阅读>></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 class="item cat_item">
                <div class="item_title"><h2><a title="'.$the_cat->name.'" href="'.get_category_link($the_cat).'">'.$the_cat->name.'</a></h2></div>
                <ul class="box_list">';
                    foreach($posts as $post){
                        echo '<li><span class="alignright">'.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>';
        }
    }
}
?>
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
wordpress教程

wordpress教程之 WordPress 删除类别

2023-2-16 23:47:37

wordpress教程

无插件实现WordPress后台直接显示文章、页面、分类、标签和用户等ID号

2023-2-16 23:47:40

!
你也想出现在这里?立即 联系我们吧!
信息
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
搜索