WordPress函数自制之关于Post,Page,Category之间关系的函数
发布时间: 2011-04-13有一条回复
之前你一定看过这篇关于如何判断文章是否属于某分类的WordPress函数的博文,事实上,做WordPress主题的过程中,在Post与Category之间,Category与Category之间,Page与Page之间常常都会碰到需要表明彼此关系或判断归属的情况,而每一次也都可以通过WordPress内置的一些函数进行判断,但因为都会用到不止一个函数,这样使得整个过程显得非常繁琐,如果应用到的地方很多时,那简直就是一件令人十分痛苦的事情.所以今天你将很幸运地看到我提供了一系列相关的自制WordPress函数:
之前有写过的post_is_in_under_category函数,用于判断文章是否归属于某一分类或其子分类:
//post_is_in_under_category //check if a post is under a category or categories; //In Single.php,second param no need; //first param is for cat id; function post_is_in_under_category( $cats, $_post = null ) { foreach ( (array) $cats as $cat ) { // get_term_children() accepts integer ID only $descendants = get_term_children( (int) $cat, 'category'); if (in_category( $cat, $_post ) || ($descendants && in_category( $descendants, $_post) ) ) return true; } return false; }
post_parent_id函数,用于获取文章的第一个直接父分类ID:
//post_parent_id //to get the post parent cat ID; function post_parent_id($postid=null) { if (!$postid){ global $post; $postid=$post->ID; } $c=get_the_category($postid); return $c[0]->cat_ID; }
post_root_id函数,用于获取文章的根分类ID:
//post_root_id //to get the post root cat ID; function post_root_id($postid=null) { if (!$postid){ global $post; $postid=$post->ID; } $c=get_the_category($postid); return cat_root_id($c[0]->cat_ID); }
cat_is_in_under_category函数,用于判断某一分类是否归属于另一分类或就是这一分类本身:
//cat_is_in_under_category //check if a cat is under another function cat_is_in_under_category($category,$cat=null) { $is_in_under_category=0; if ($cat==null){ global $cat; } if ($category==$cat){$is_in_under_category=1;} $descendants = get_term_children( $category, 'category'); foreach ($descendants as $desc){ if ($cat==$desc){$is_in_under_category=1;} } return $is_in_under_category; }
cat_parent_id函数,用于获取某一分类的父分类ID:
//cat_parent_id //to get the cat parent cat ID, if none return the cat ID; function cat_parent_id($catid=null) { if ($cat==null){ global $cat; $catid=$cat; } $this_cat=get_term($catid,'category'); if($this_cat->parent!=0){ $catid=$this_cat->parent; } return $catid; }
cat_root_id函数,用于获取某一分类的根分类ID:
//cat_root_id //to get the cat root cat ID, if none return the cat ID; function cat_root_id($catid=null) { if ($catid==null){ global $cat; $catid=$cat; } $this_cat=get_term($catid,'category'); $this_cat_parent=$this_cat->parent; while ($this_cat_parent!=0){ $catid=$this_cat->parent; $this_cat=get_term($catid,'category'); $this_cat_parent=$this_cat->parent; } return $catid; }
page_is_in_under_page函数,用于判断某一Page是否是另一Page本身或归属其子Page:
//page_is_in_under_page //check if a page is under another page function page_is_in_under_page($parent_page,$pageid=null) { if (!$pageid){ global $post; $pageid=$post->ID; } if ($pageid==$parent_page){ return true; } $this_page=get_post($pageid); $this_page_parent=$this_page->post_parent; if ($this_page_parent==$parent_page){ return true; } while ($this_page_parent!=0){ $pageid=$this_page_parent; $this_page=get_post($pageid); $this_page_parent=$this_page->post_parent; if ($this_page_parent==$parent_page){ return true; } } return false; }
page_parent_id函数,用于获取某一Page的直属父Page的ID:
//page_parent_id //to get the parent page id, if none return the page id; function page_parent_id($pageid=null) { if (!$pageid){ global $post; $pageid=$post->ID; } $this_page=get_post($pageid); $this_page_parent=$this_page->post_parent; if($this_page_parent!=0){ $pageid=$this_page_parent; } return $pageid; }
page_root_id函数,用于获取某一Page的根Page的ID:
//page_root_id //to get the root parent page id, if none return the page id; function page_root_id($pageid=null) { if (!$pageid){ global $post; $pageid=$post->ID; } $this_page=get_post($pageid); $this_page_parent=$this_page->post_parent; while ($this_page_parent!=0){ $pageid=$this_page_parent; $this_page=get_post($pageid); $this_page_parent=$this_page->post_parent; } return $pageid; }
这些函数定义在Lizus定制主题的core-spark/functions/spark_functions.php中,所以,如果你使用Lizus的定制WordPress主题,都可以直接在主题代码中使用这些函数,这些函数的用途也正如函数名称所写,因为都比较直接,所以我并没有写很多注释.相信你一定看得懂的.
这些函数都不属于必须函数,有些人会担心性能问题,但我在实际使用中并未发现到明显不同,而另一方面它们能带来便利,相信值得一试.