wp_unique_term_slug

函数
wp_unique_term_slug ( $slug, $term )
参数
  • (string) $slug The string that will be tried for a unique slug.
    Required:
  • (object) $term The term object that the `$slug` will belong to.
    Required:
返回值
  • (string) Will return a true unique slug.
定义位置
相关方法
wp_unique_post_slugwp_count_termswp_unique_idwp_enqueue_stylewp_update_term_count
引入
2.3.0
弃用
-

wp_unique_term_slug是一个WordPress函数,它为一个术语生成一个独特的slug。在WordPress中,术语是用来对文章进行分类和分组的标签。

使术语的slug唯一化,如果它还不是的话。

`$slug’必须对每个分类法都是唯一的,这意味着一个分类法术语不能与另一个分类法术语的slug相匹配。每个lug对每个分类法都必须是全球唯一的。

其工作方式是,如果该术语所属的分类法是分层的,并且有一个父类,它将把该父类附加到$slug上。

如果这仍然不能返回一个唯一的slug,那么它就会尝试添加一个数字,直到找到一个真正唯一的数字。

`$term’的唯一目的是追加一个父类,如果存在的话。

function wp_unique_term_slug( $slug, $term ) {
	global $wpdb;

	$needs_suffix  = true;
	$original_slug = $slug;

	// As of 4.1, duplicate slugs are allowed as long as they're in different taxonomies.
	if ( ! term_exists( $slug ) || get_option( 'db_version' ) >= 30133 && ! get_term_by( 'slug', $slug, $term->taxonomy ) ) {
		$needs_suffix = false;
	}

	/*
	 * If the taxonomy supports hierarchy and the term has a parent, make the slug unique
	 * by incorporating parent slugs.
	 */
	$parent_suffix = '';
	if ( $needs_suffix && is_taxonomy_hierarchical( $term->taxonomy ) && ! empty( $term->parent ) ) {
		$the_parent = $term->parent;
		while ( ! empty( $the_parent ) ) {
			$parent_term = get_term( $the_parent, $term->taxonomy );
			if ( is_wp_error( $parent_term ) || empty( $parent_term ) ) {
				break;
			}
			$parent_suffix .= '-' . $parent_term->slug;
			if ( ! term_exists( $slug . $parent_suffix ) ) {
				break;
			}

			if ( empty( $parent_term->parent ) ) {
				break;
			}
			$the_parent = $parent_term->parent;
		}
	}

	// If we didn't get a unique slug, try appending a number to make it unique.

	/**
	 * Filters whether the proposed unique term slug is bad.
	 *
	 * @since 4.3.0
	 *
	 * @param bool   $needs_suffix Whether the slug needs to be made unique with a suffix.
	 * @param string $slug         The slug.
	 * @param object $term         Term object.
	 */
	if ( apply_filters( 'wp_unique_term_slug_is_bad_slug', $needs_suffix, $slug, $term ) ) {
		if ( $parent_suffix ) {
			$slug .= $parent_suffix;
		}

		if ( ! empty( $term->term_id ) ) {
			$query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s AND term_id != %d", $slug, $term->term_id );
		} else {
			$query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $slug );
		}

		if ( $wpdb->get_var( $query ) ) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
			$num = 2;
			do {
				$alt_slug = $slug . "-$num";
				$num++;
				$slug_check = $wpdb->get_var( $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $alt_slug ) );
			} while ( $slug_check );
			$slug = $alt_slug;
		}
	}

	/**
	 * Filters the unique term slug.
	 *
	 * @since 4.3.0
	 *
	 * @param string $slug          Unique term slug.
	 * @param object $term          Term object.
	 * @param string $original_slug Slug originally passed to the function for testing.
	 */
	return apply_filters( 'wp_unique_term_slug', $slug, $term, $original_slug );
}

常见问题

FAQs
查看更多 >