獲取文章頁(yè)面上級(jí)目錄或下級(jí)目錄時(shí)會(huì)用到下面的程序了,下面我分別與各位同學(xué)一看看調(diào)用上下級(jí)分類目錄程序代碼.在當(dāng)前分類或者正文頁(yè)面想調(diào)用顯示與當(dāng)前分類存在父子關(guān)系的分類目錄時(shí)會(huì)用到.
代碼一:將下面代碼加到主題模板適當(dāng)位置,比如側(cè)邊欄:
- <?php
- $current = "";
- if(is_single()){
- $parent = get_the_category();
- $parent = $parent[0];
- $current = "¤t_category=".$parent->term_id;
- }else if(is_category()){
- global $cat;
- $parent = get_category($cat);
- }
- if($parent->category_parent != 0){
- $cat_id = $parent->category_parent;
- $parent = get_category($cat_id);
- if($parent->category_parent != 0){
- $cat_id = $parent->category_parent;
- }else{
- $cat_id = $parent->term_id;
- }
- }else{
- $cat_id = $parent->term_id;
- }
- ?>
- <?php if(!is_page()) { ?>
- <h3><?php echo $parent->cat_name; ?><span><?php echo $parent->slug; ?></span></h3>
- <ul id="cat_list">
- <?php wp_list_categories("title_li=&child_of=$cat_id".$current); ?>
- </ul>
- <?php } ?>
代碼二:將下面代碼加到主題function.php模板文件中:
- function get_category_root_id($cat)
- {
- $this_category = get_category($cat); // 取得當(dāng)前分類
- while($this_category->category_parent) // 若當(dāng)前分類有上級(jí)分類時(shí),循環(huán)
- {
- $this_category = get_category($this_category->category_parent); // 將當(dāng)前分類設(shè)為上級(jí)分類(往上爬)
- }
- return $this_category->term_id; // 返回根分類的id號(hào)
- }
調(diào)用顯示代碼加到主題模板的適當(dāng)位置,代碼如下:
- <?php
- if(is_category())
- {
- if(get_category_children(get_category_root_id(the_category_ID(false)))!= "" )
- {
- echo '<ul>';
- echo wp_list_categories("child_of=".get_category_root_id(the_category_ID(false)). "&depth=0&hide_empty=0&title_li=&orderby=id&order=ASC");
- echo '</ul>';
- }
- }
- ?>
如果我們要調(diào)用當(dāng)前目錄下文章怎么調(diào)用?在想要調(diào)用分類最新文章列表的地方添加以下代碼:
- <?php
- query_posts('showposts=1&cat=3');
- while(have_posts()) : the_post();
- ?>
- <ul>
- <li><h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
- <ul><li><?php the_content(); ?></li>
- </ul>
- </li>
- </ul>
- <?php endwhile; ?>
其中showposts后面的數(shù)碼表示顯示的文章數(shù)量,cat后面的數(shù)值表示分類ID,調(diào)用wordpress指定文章,代碼如下:
- <?php query_posts('p=1'); ?>
- <?php while (have_posts()) : the_post(); ?>
- <a href="<?php the_permalink(); ?>"><?php the_content(); ?></a>
- <?php endwhile;wp_reset_query();?>
新聞熱點(diǎn)
疑難解答
圖片精選