国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網(wǎng)站 > 建站經(jīng)驗(yàn) > 正文

WordPress按分類顯示文章的方法

2024-04-25 20:42:34
字體:
供稿:網(wǎng)友

可以自動將WordPress站點(diǎn)的文章按分類顯示在一個頁面中,用作CMS主題布局很實(shí)用。

用下代碼替換首頁模板主循環(huán),則按分類顯示各分類最新的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>';

}

}

?>

用下代碼替換分類歸檔模板主循環(huán),則顯示該分類下所有子分類的最新5篇文章,并按分類顯示。如果當(dāng)前分類下不存在子分類,則顯示該分類的文章列表!

<?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'); ?> | 標(biāo)簽: <?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>';

}

}

}

?>

以上就是關(guān)于WordPress按分類顯示文章的方法,希望對大家有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 金坛市| 驻马店市| 邓州市| 高唐县| 通辽市| 苍南县| 东台市| 肇东市| 郑州市| 郁南县| 佛冈县| 湖口县| 鞍山市| 饶阳县| 三亚市| 龙南县| 德格县| 丰都县| 永吉县| 揭东县| 贞丰县| 清徐县| 郧西县| 新乡市| 淮南市| 永安市| 平山县| 荣成市| 湖口县| 新宾| 达州市| 西畴县| 射洪县| 绥芬河市| 城市| 闸北区| 神池县| 永寿县| 玛纳斯县| 阳原县| 龙陵县|