wp_unique_term_slug [ WordPress Function ]
| Parameters: |
|
| Uses: |
|
| Returns: |
|
| Defined at: |
|
Will make slug unique, if it isn't already.
The $slug has to be unique global to every taxonomy, meaning that one taxonomy term can't have a matching slug with another taxonomy term. Each slug has to be globally unique for every taxonomy.
The way this works is that if the taxonomy that the term belongs to is hierarchical and has a parent, it will append that parent to the $slug.
If that still doesn't return an unique slug, then it try to append a number until it finds a number that is truly unique.
The only purpose for $term is for appending a parent, if one exists.
Source
<?php
function wp_unique_term_slug($slug, $term) {
global $wpdb;
if ( ! term_exists( $slug ) )
return $slug;
// If the taxonomy supports hierarchy and the term has a parent, make the slug unique
// by incorporating parent slugs.
if ( 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;
$slug .= '-' . $parent_term->slug;
if ( ! term_exists( $slug ) )
return $slug;
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.
if ( !empty($args['term_id']) )
$query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s AND term_id != %d", $slug, $args['term_id'] );
else
$query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $slug );
if ( $wpdb->get_var( $query ) ) {
$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;
}
return $slug;
}
?>
Examples [ wp-snippets.com ]
Google Arama Sonuçlarý
- wp_unique_term_slug() WordPress function reference, arguments ...
wp_unique_term_slug(). Will make slug unique, if it isn't already. The $slug has to be unique global to every taxonomy, meaning that one taxonomy term can't ...
queryposts.com - Function Reference/wp unique term slug « WordPress Codex
... Hosting · Download. Codex. Codex tools: Log in. Function Reference/wp unique term slug. This function exists. The documentation of it in the code is flawed.
codex.wordpress.org - #13344 (Nav Menu slugs are incremented on each save ...
... t WHERE t.slug = 'main' Time: 0.000410079956055 Caller: wp_update_nav_menu_object, wp_update_term, wp_unique_term_slug, is_term Query: SELECT ...
core.trac.wordpress.org - Docs for page functions.wp-taxonomy.php
$object_id; $terms; $taxonomy; $append. wp_unique_term_slug (line 117). void wp_unique_term_slug ( $slug, $term). $slug; $term. wp_update_term (line 122) ...
phpdoc.ftwr.co.uk