好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

WordPress当前页面调用上下级分类目录 - WordPress

WordPress当前页面调用上下级分类目录

获取文章页面上级目录或下级目录时会用到下面的程序了,下面我分别与各位同学一看看调用上下级分类目录程序代码.在当前分类或者正文页面想调用显示与当前分类存在父子关系的分类目录时会用到.

代码一: 将下面代码加到主题模板适当位置,比如侧边栏:

<?php           $current  =  "" ;           if (is_single()){               $parent  = get_the_category();               $parent  =  $parent [0];               $current  =  "&current_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 );  // 取得当前分类            while ( $this_category ->category_parent)  // 若当前分类有上级分类时,循环           {               $this_category  = get_category( $this_category ->category_parent);  // 将当前分类设为上级分类(往上爬)           }           return   $this_category ->term_id;  // 返回根分类的id号   } 

调用显示代码加到主题模板的适当位置,代码如下:

<?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>' ;              }          }  ?> 

如果我们要调用当前目录下文章怎么调用?在想要调用分类最新文章列表的地方添加以下代码:

<?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后面的数码表示显示的文章数量,cat后面的数值表示分类ID,调用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();?> 

查看更多关于WordPress当前页面调用上下级分类目录 - WordPress的详细内容...

  阅读:66次