个性化的WordPress原创主题

WordPress文章企业网站主题模板
WordPress主题建站 > WordPress标签 > WordPress根据博客ID取得当前博客文章所在的分类 get_the_term_list

WordPress根据博客ID取得当前博客文章所在的分类 get_the_term_list

责任编辑:玲熙 更新时间: 关注:546

今天在使用WordPress建站中,使用到了get_the_term_list函数,体会到了实用性及便捷;接下来协典筒为您介绍get_the_term_list函数的使用。

  1. get_the_term_list($id,$taxonomy,$before='',$sep='',$after= '')

使用方式

  1. echo get_the_term_list($post->ID,"category");
  1. get_the_term_list()位于 /wp-includes/category-template.php 1326行[6.0版本]
  1. /**
  2.  * Retrieves a post's terms as a list with specified format.
  3.  *
  4.  * Terms are linked to their respective term listing pages.
  5.  *
  6.  * @since 2.5.0
  7.  *
  8.  * @param int    $post_id  Post ID.
  9.  * @param string $taxonomy Taxonomy name.
  10.  * @param string $before   Optional. String to use before the terms. Default empty.
  11.  * @param string $sep      Optional. String to use between the terms. Default empty.
  12.  * @param string $after    Optional. String to use after the terms. Default empty.
  13.  * @return string|false|WP_Error A list of terms on success, false if there are no terms,
  14.  *                               WP_Error on failure.
  15.  */
  16. function get_the_term_list( $post_id, $taxonomy, $before = '', $sep = '', $after = '' ) {
  17. $terms = get_the_terms( $post_id, $taxonomy );

  18. if ( is_wp_error( $terms ) ) {
  19. return $terms;
  20. }

  21. if ( empty( $terms ) ) {
  22. return false;
  23. }

  24. $links = array();

  25. foreach ( $terms as $term ) {
  26. $link = get_term_link( $term, $taxonomy );
  27. if ( is_wp_error( $link ) ) {
  28. return $link;
  29. }
  30. $links[] = '' . $term->name . '';
  31. }

  32. /**
  33.  * Filters the term links for a given taxonomy.
  34.  *
  35.  * The dynamic portion of the hook name, `$taxonomy`, refers
  36.  * to the taxonomy slug.
  37.  *
  38.  * Possible hook names include:
  39.  *
  40.  *  - `term_links-category`
  41.  *  - `term_links-post_tag`
  42.  *  - `term_links-post_format`
  43.  *
  44.  * @since 2.5.0
  45.  *
  46.  * @param string[] $links An array of term links.
  47.  */
  48. $term_links = apply_filters( "term_links-{$taxonomy}", $links );  // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores

  49. return $before . implode( $sep, $term_links ) . $after;
  50. }


  • 获赞8
  • 声明:内容版权归原作者所有,未经授权不得任意转载
  • 本文标题和链接:
    WordPress根据博客ID取得当前博客文章所在的分类 get_the_term_list
    https://e.69525.com/f/5ce97959326bd76b/

相关阅读

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

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