wordpress子分类文章列表调用方法
在做wordpress cms主题中常常需要在分类页中调用子分类和分类列表,如果不存在子分类就显示文章列表。
在wordpress主题文件category.php加入以下代码即可实现,
具体样式根据网站结构自行调整。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
<?php global $cat; $cats = get_categories(array( 'child_of' => $cat, 'parent' => $cat, 'hide_empty' => 0 )); $c = get_category($cat); if(empty($cats)){ ?> <div class="infoList"> <ul class="infoListUL"> <?php if (have_posts()) : while (have_posts()) : the_post(); ?> <li> <a href="<?php the_permalink(); ?>" class="articleTitle fl" target="_blank"><?php the_title(); ?></a> </li> <?php endwhile; ?> <?php else: ?> </ul></div> <?php endif; ?> </div> <div class="dede_pages"> <ul class="pagelist"> <?php pagenavi(); ?> </ul> </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="typeBox"> <h2 class="zuojia_title"><a title="'.$the_cat->name.'" href="'.get_category_link($the_cat).'">'.$the_cat->name.'</a></h2><a href="'.get_category_link($the_cat).'" class="more">更多>> </a> <ul class="typeList1">'; foreach($posts as $post){ echo '<li> <a title="'.$post->post_title.'" href="'.get_permalink($post->ID).'">'.$post->post_title.'</a></li>'; } echo '</ul> </div>'; } } } ?> |
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END