WordPress函数文档get_cat_ID()
通过分类名获取分类ID 描述 译文 按名称检索类别编号。 原文 Retrieve the ID of a ca…
通过分类名获取分类ID
描述
译文
按名称检索类别编号。
原文
Retrieve the ID of a category from its name.
用法
<?php get_cat_ID( $cat_name ) ?>
参数
$cat_name
(string) (可选) Default is ‘General‘ and can be any category name.
默认值: ‘General’
返回值
(integer if 0, string if ID)
0 if failure and ID of category on success.
示例
Basic
This is a very basic example of how to use this function in The_Loop. Replace “Category Name” with the name of your category.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
<?php
$category_id = get_cat_ID(‘Category Name’);
<!— Start the Loop. —>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<!— Test if the current post is in category “Category Name”. —>
<!— If it is, the div box is given the CSS class “post-cat-special”. —>
<!— Otherwise, the div box is given the CSS class “post”. —>
<?php if ( in_category($category_id) ) { ?>
<div class=“post-cat-special”>
<?php } else { ?>
<div class=“post”>
<?php } ?>
</div>
<!— Stop The Loop (but note the “else:” – see next line). —>
<?php endwhile; else: ?>
<!— The very first “if” tested to see if there were any Posts to —>
<!— display. This “else” part tells what do if there weren‘t any. —>
<p>Sorry, no posts matched your criteria.</p>
<!— REALLY stop The Loop. —>
<?php endif; ?>
|
注意
Previous Function Reference to get the category ID has been deprecated.
历史
添加于 版本: 1.0.0
源文件
get_cat_ID() 函数的代码位于 wp-includes/category.php
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
/**
* Retrieve the ID of a category from its name.
*
* @since 1.0.0
*
* @param string $cat_name Category name.
* @return int 0, if failure and ID of category on success.
*/
function get_cat_ID( $cat_name ) {
$cat = get_term_by( ‘name’, $cat_name, ‘category’ );
if ( $cat )
return $cat->term_id;
return 0;
}
|
相关
- Article(文章): Categories
- Function(函数): the_category()
- Function(函数): the_category_rss()
- Function(函数): single_cat_title()
- Function(函数): category_description()
- Function(函数): wp_dropdown_categories()
- Function(函数): wp_list_categories()
- Function(函数): get_the_category()
- Function(函数): get_the_category_by_ID()
- Function(函数): get_category_by_slug()
- Function(函数): get_the_category_list()
- Function(函数): get_category_parents()
- Function(函数): get_category_link()
- Function(函数): is_category()
- Function(函数): in_category()
- 原文:http://codex.wordpress.org/Function_Reference/get_cat_ID
类别:WordPress函数文档、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
评论功能已经关闭!