个性化的WordPress原创主题

WordPress文章企业网站主题模板
WordPress主题建站 > WordPress标签 > WordPress根据分类ID读取分类信息及分类链接

WordPress根据分类ID读取分类信息及分类链接

责任编辑:初霞 更新时间: 关注:578

WordPress读取分类信息可以使用到以下标签:get_cat_name、get_category、get_category_link;

其中:

get_cat_name,只输出分类名称;

get_category_link,只输出分类链接;

get_category,则输出分类信息,其中包括分类描述、目录、分类名称等;

详细使用方法如下。

  1. 读取分类名称:
  2. <?php echo get_cat_name($cat_id); ?>
  3. 该标签会输出如:
  4. WordPress标签
  1. 读取分类链接:
  2. <?php echo get_category_link($cat_id); ?>
  3. 该标签会输出如:
  4. https://e.69525.com/f/
  1. 读取分类信息:
  2. <?php echo get_category($cat_id); ?>
  3. 该标签会输出如:
  4. WP_Term Object
  5. (
  6.     [term_id] => 52
  7.     [name] => WordPress标签
  8.     [slug] => f
  9.     [term_group] => 0
  10.     [term_taxonomy_id] => 23
  11.     [taxonomy] => category
  12.     [description] =>
  13.     [parent] => 0
  14.     [count] => 0
  15.     [filter] => raw
  16.     [cat_ID] => 52
  17.     [category_count] => 0
  18.     [category_description] =>
  19.     [cat_name] => WordPress标签
  20.     [category_nicename] => f
  21.     [category_parent] => 0
  22. )
  1. get_category()位于 /wp-includes/category.php 91行[6.0版本]
  1. /**
  2.  * Retrieves category data given a category ID or category object.
  3.  *
  4.  * If you pass the $category parameter an object, which is assumed to be the
  5.  * category row object retrieved the database. It will cache the category data.
  6.  *
  7.  * If you pass $category an integer of the category ID, then that category will
  8.  * be retrieved from the database, if it isn't already cached, and pass it back.
  9.  *
  10.  * If you look at get_term(), then both types will be passed through several
  11.  * filters and finally sanitized based on the $filter parameter value.
  12.  *
  13.  * @since 1.5.1
  14.  *
  15.  * @param int|object $category Category ID or category row object.
  16.  * @param string     $output   Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
  17.  *                             correspond to a WP_Term object, an associative array, or a numeric array,
  18.  *                             respectively. Default OBJECT.
  19.  * @param string     $filter   Optional. How to sanitize category fields. Default 'raw'.
  20.  * @return object|array|WP_Error|null Category data in type defined by $output parameter.
  21.  *                                    WP_Error if $category is empty, null if it does not exist.
  22.  */
  23. function get_category( $category, $output = OBJECT, $filter = 'raw' ) {
  24. $category = get_term( $category, 'category', $output, $filter );

  25. if ( is_wp_error( $category ) ) {
  26. return $category;
  27. }

  28. _make_cat_compat( $category );

  29. return $category;
  30. }
  1. get_cat_name()位于 /wp-includes/category.php 223行[6.0版本]
  1. /**
  2.  * Retrieves the name of a category from its ID.
  3.  *
  4.  * @since 1.0.0
  5.  *
  6.  * @param int $cat_id Category ID.
  7.  * @return string Category name, or an empty string if the category doesn't exist.
  8.  */
  9. function get_cat_name( $cat_id ) {
  10. $cat_id   = (int) $cat_id;
  11. $category = get_term( $cat_id, 'category' );

  12. if ( ! $category || is_wp_error( $category ) ) {
  13. return '';
  14. }

  15. return $category->name;
  16. }
  1. get_category_link()位于 /wp-includes/category-template.php 20行[6.0版本]
  1. /**
  2.  * Retrieves category link URL.
  3.  *
  4.  * @since 1.0.0
  5.  *
  6.  * @see get_term_link()
  7.  *
  8.  * @param int|object $category Category ID or object.
  9.  * @return string Link on success, empty string if category does not exist.
  10.  */
  11. function get_category_link( $category ) {
  12. if ( ! is_object( $category ) ) {
  13. $category = (int) $category;
  14. }

  15. $category = get_term_link( $category );

  16. if ( is_wp_error( $category ) ) {
  17. return '';
  18. }

  19. return $category;
  20. }
  • 获赞9
  • 声明:内容版权归原作者所有,未经授权不得任意转载
  • 本文标题和链接:
    WordPress根据分类ID读取分类信息及分类链接
    https://e.69525.com/f/77b997b0efd45010/

相关阅读

Copyright © 2023 WordPress主题. All rights reserved.Powered by e.69525.com.

本站基于WordPress主题搭建,正在以新的版本流畅运行;由69525提供主题免费升级支持