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

首頁(yè) > CMS > Wordpress > 正文

WordPress當(dāng)前頁(yè)面調(diào)用上下級(jí)分類目錄

2024-09-07 00:50:26
字體:
供稿:網(wǎng)友

獲取文章頁(yè)面上級(jí)目錄或下級(jí)目錄時(shí)會(huì)用到下面的程序了,下面我分別與各位同學(xué)一看看調(diào)用上下級(jí)分類目錄程序代碼.在當(dāng)前分類或者正文頁(yè)面想調(diào)用顯示與當(dāng)前分類存在父子關(guān)系的分類目錄時(shí)會(huì)用到.

代碼一:將下面代碼加到主題模板適當(dāng)位置,比如側(cè)邊欄:

  1. <?php 
  2.         $current = ""
  3.         if(is_single()){ 
  4.             $parent = get_the_category(); 
  5.             $parent = $parent[0]; 
  6.             $current = "&current_category=".$parent->term_id; 
  7.         }else if(is_category()){ 
  8.             global $cat
  9.             $parent = get_category($cat); 
  10.         } 
  11.         if($parent->category_parent != 0){ 
  12.             $cat_id = $parent->category_parent; 
  13.             $parent = get_category($cat_id); 
  14.             if($parent->category_parent != 0){ 
  15.                 $cat_id = $parent->category_parent; 
  16.             }else
  17.                 $cat_id = $parent->term_id; 
  18.             } 
  19.         }else
  20.             $cat_id = $parent->term_id; 
  21.         } 
  22.     ?> 
  23.     <?php if(!is_page()) { ?> 
  24.         <h3><?php echo $parent->cat_name; ?><span><?php echo $parent->slug; ?></span></h3> 
  25.         <ul id="cat_list"
  26.             <?php wp_list_categories("title_li=&child_of=$cat_id".$current); ?>    
  27.         </ul> 
  28.     <?php } ?> 

代碼二:將下面代碼加到主題function.php模板文件中:

  1. function get_category_root_id($cat
  2.     { 
  3.         $this_category = get_category($cat); // 取得當(dāng)前分類 
  4.         while($this_category->category_parent) // 若當(dāng)前分類有上級(jí)分類時(shí),循環(huán) 
  5.         { 
  6.             $this_category = get_category($this_category->category_parent); // 將當(dāng)前分類設(shè)為上級(jí)分類(往上爬) 
  7.         } 
  8.         return $this_category->term_id; // 返回根分類的id號(hào) 

調(diào)用顯示代碼加到主題模板的適當(dāng)位置,代碼如下:

  1. <?php 
  2.         if(is_category()) 
  3.         { 
  4.             if(get_category_children(get_category_root_id(the_category_ID(false)))!= "" ) 
  5.             { 
  6.                 echo '<ul>'
  7.                 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"); 
  8.                 echo '</ul>'
  9.             } 
  10.         } 
  11. ?> 

如果我們要調(diào)用當(dāng)前目錄下文章怎么調(diào)用?在想要調(diào)用分類最新文章列表的地方添加以下代碼:

  1. <?php 
  2. query_posts('showposts=1&cat=3'); 
  3. while(have_posts()) : the_post(); 
  4. ?> 
  5. <ul> 
  6. <li><h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3> 
  7. <ul><li><?php the_content(); ?></li> 
  8. </ul> 
  9. </li> 
  10. </ul> 
  11. <?php endwhile; ?> 

其中showposts后面的數(shù)碼表示顯示的文章數(shù)量,cat后面的數(shù)值表示分類ID,調(diào)用wordpress指定文章,代碼如下:

  1. <?php query_posts('p=1'); ?> 
  2. <?php while (have_posts()) : the_post(); ?> 
  3. <a href="<?php the_permalink(); ?>"><?php the_content(); ?></a> 
  4. <?php endwhile;wp_reset_query();?> 

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宁晋县| 兰坪| 明水县| 沾化县| 乌拉特中旗| 台湾省| 仙桃市| 中超| 平度市| 始兴县| 察隅县| 清水河县| 祁阳县| 孝昌县| 枣庄市| 兰坪| 靖安县| 盘山县| 大竹县| 延川县| 密山市| 苍溪县| 建平县| 象山县| 达尔| 大城县| 溧水县| 东至县| 龙口市| 绥芬河市| 新民市| 大丰市| 杨浦区| 砚山县| 西宁市| 仁化县| 德州市| 贡山| 万荣县| 郑州市| 固镇县|