学做网站培训课程介绍

当前位置:

WordPress父分类调用子分类名称和文章列表

在之前的网站制作文章中讲到了wordpress如何调用当前分类下面的子分类的方法,但这种方法只能调用出子分类的名称,无法去调用出子分类下面的文章列表。(相关阅读:wordpress调用分类下的二级分类名

在做网站时,为了实现某种需要,我们要同时调用出分类的名称和文章列表,以达到如下图的效果。
wordpress同时调用子分类的名称和文章列表

为达到这样的功能,我们需要判断一下当前分类包含的二级子分类,然后调用出所有的二级分类ID,再通过ID去调用子分类的名称和子分类下面的文章列表。代码如下:

<?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(); ?>">more >></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>'
;
}
}
}
?>

还可以调用指定分类下所有子分类及文章列表,代码如下:


<?php
$cats = get_categories(array(
'child_of' => $catid01,
'parent' => $catid01,
'hide_empty' => 0
));

if(!empty($cats)){
foreach($cats as $the_cat){
$posts = get_posts(array(
'category' => $the_cat->cat_ID,
'numberposts' => 10,
));
if(!empty($posts)){

echo '
<div class="cat_item">
<h2><a title="'
.$the_cat->name.'" href="'.get_category_link($the_cat).'">'.$the_cat->name.'</a></h2>
<ul class="box_list">'
;

foreach($posts as $post){?>

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

<?php
}
echo '</ul>
</div>'
;

}
}
}
?>

通过对以上的代码进行改编辑,结合WordPress分类目录添加缩略图上传功能,可以制作出以下的网页效果。

wordpress同时调用子分类的名称和文章列表扩展

代码如下:(代码里的分类ID和图片的地址自行修改)


<?php
global $cat;
$cats = get_categories(array(
'child_of' => $cat,
'parent' => $cat,
'hide_empty' => 0
));
$c = get_category($cat);
if(empty($cats)){
?>
<?php
}else{
foreach($cats as $the_cat){
$posts = get_posts(array(
'category' => $the_cat->cat_ID,
'numberposts' => 6,
));
if(!empty($posts)){
echo '
<tr bgcolor="#006699" align="center">
<td colspan="2" height="30" align="left"><font size="2" color="#ffffff">&gt;&gt;&gt; '
.$the_cat->name.'</font></td>
</tr>
<tr bgcolor="#edf1f5">
<td width="40%" height="25" align="center"><a title="'
.$the_cat->name.'" href="'.get_category_link($the_cat).'" target="_blank"><img alt="'.$the_cat->name.'" src="https://www.xuewangzhan.com/wp-content/themes/jcw/images/'.$the_cat->cat_ID.'.jpg" width="250"></a></td>
<td width="60%">
<table cellspacing="0" cellpadding="0" border="0">
<tbody>'
;
foreach($posts as $post){
echo '

<tr height="22">
<td width="80%"><a href="'
.get_permalink($post->ID).'" target="_blank">'.$post->post_title.'</a></td>
<td width="20%">'
.mysql2date('Y-m-d', $post->post_date).'</td>
</tr> '
;
}
echo '
<tr height="1">
<td colspan="2" width="100%"><hr style="border:1px #cccccc dotted" size="1" noshade="noshade"></td>
</tr>
<tr>
<td colspan="2" width="100%"><a title="'
.$the_cat->name.'" href="'.get_category_link($the_cat).'" target="_blank"><img src="https://www.xuewangzhan.com/wp-content/themes/jcw/images/a0.gif" width="200" height="46" border="0"></a></td>
</tr>
</tbody></table>
</td></tr>
<tr bgcolor="#ffffff">
<td height="5" align="center"></td>
<td></td></tr>'
;
}
}
}
?>

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

相关教程

  • 用什么建网站?Wordpress建网站好不好?这些问题经常被新手问上问起。可以这样说,wordpress程序可以建各种类型的网站,它的功能强大之处令人称赞,
  • Wordpress 如何开启伪静态 视频教程 (1120063 次浏览)
    做网站制作好之后,如果网站做SEO优化时,需要进行伪静态的设置,同样也可以通过网站设置选置进行设置。Wordpress伪静态设置方法 登陆到wordpr
  • wordpress  在写文章之前需要先创建分类目录,那么怎么样去创建分类目录呢?Wordpress创建分类目录步骤 首先登陆wordpress网站后台,鼠标悬停
  • 这是一个综合性的门户网站,并且含有分类信息功能。网站功能丰富,有新闻展示、图片展示、视频展示等版块。另外在网站首页可以设置大量的广告位,
  • 我们在浏览网站时,会发现有些网站的顶部会有天气预报功能,并且可以根据用户所在的城市自动变换成当地的天气预报信息。那么我们在自己建网站时,怎么
  • 我们在学习wordpress模板制作教程时,讲到了如何制作网站的菜单功能。今天我们讲一下如何添加wordpress后台菜单设置选项,通过后台菜单选项就可以轻松
  • 在企业网站制作时,需要在网站的边栏列出企业产品分类,如果公司网站产品项目很多的情况,就需要设置二级分类或者子分类。如下图:如何在自己
  • 在使用wordpress建网站时,往往需要在侧边栏调用当前栏目的子分类子目录,而且在点击子目录时,还会侧边栏显示这些子目录的分类。在wp程序下,
  • 学做网站时,经常要修改代码,很多时候需要用到颜色代码,由于太多,不可能都记住,所以找到了一个带有中文颜色名称表示的颜色代码表,以便后期网页设
  • 问:老师你好,我学习了网站制作课程,然后自己用wordpress程序做了一个网站,在网站的后台添加了几个分类,有一个分类下面还包含了几个子分类,我想
  • 在之前的网站制作文章中讲到了wordpress如何调用当前分类下面的子分类的方法,但这种方法只能调用出子分类的名称,无法去调用出子分类下面的文章列表
  • 列表标签有二种,分为有序列表(ol标签)和无序列表(ul标签)。通过列表标签可以排列网站中的很多内容,让用户一目了然。列表标签在做网站和网页