wp_check_term_hierarchy_for_loops

函式
wp_check_term_hierarchy_for_loops ( $parent, $term_id, $taxonomy )
引數
  • (int) $parent `term_id` of the parent for the term we're checking.
    Required:
  • (int) $term_id The term we're checking.
    Required:
  • (string) $taxonomy The taxonomy of the term we're checking.
    Required:
返回值
  • (int) The new parent for the term.
定義位置
相關方法
wp_check_post_hierarchy_for_loops_get_term_hierarchywp_find_hierarchy_loopwp_check_term_meta_support_prefilterwp_check_widget_editor_deps
引入
3.1.0
棄用
-

wp_check_term_hierarchy_for_loops: 這是一個檢查術語層次結構中的迴圈的函式。它可以用來防止無限迴圈,當術語被組織成一個層次結構時,可能會出現無限迴圈。

檢查術語層次結構的給定子集是否有層次結構迴圈。

防止迴圈的形成,並打破那些它發現的迴圈。

附在{@see ‘wp_update_term_parent’}過濾器上。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function wp_check_term_hierarchy_for_loops( $parent, $term_id, $taxonomy ) {
// Nothing fancy here - bail.
if ( ! $parent ) {
return 0;
}
// Can't be its own parent.
if ( $parent === $term_id ) {
return 0;
}
// Now look for larger loops.
$loop = wp_find_hierarchy_loop( 'wp_get_term_taxonomy_parent_id', $term_id, $parent, array( $taxonomy ) );
if ( ! $loop ) {
return $parent; // No loop.
}
// Setting $parent to the given value causes a loop.
if ( isset( $loop[ $term_id ] ) ) {
return 0;
}
// There's a loop, but it doesn't contain $term_id. Break the loop.
foreach ( array_keys( $loop ) as $loop_member ) {
wp_update_term( $loop_member, $taxonomy, array( 'parent' => 0 ) );
}
return $parent;
}
function wp_check_term_hierarchy_for_loops( $parent, $term_id, $taxonomy ) { // Nothing fancy here - bail. if ( ! $parent ) { return 0; } // Can't be its own parent. if ( $parent === $term_id ) { return 0; } // Now look for larger loops. $loop = wp_find_hierarchy_loop( 'wp_get_term_taxonomy_parent_id', $term_id, $parent, array( $taxonomy ) ); if ( ! $loop ) { return $parent; // No loop. } // Setting $parent to the given value causes a loop. if ( isset( $loop[ $term_id ] ) ) { return 0; } // There's a loop, but it doesn't contain $term_id. Break the loop. foreach ( array_keys( $loop ) as $loop_member ) { wp_update_term( $loop_member, $taxonomy, array( 'parent' => 0 ) ); } return $parent; }
function wp_check_term_hierarchy_for_loops( $parent, $term_id, $taxonomy ) {
	// Nothing fancy here - bail.
	if ( ! $parent ) {
		return 0;
	}

	// Can't be its own parent.
	if ( $parent === $term_id ) {
		return 0;
	}

	// Now look for larger loops.
	$loop = wp_find_hierarchy_loop( 'wp_get_term_taxonomy_parent_id', $term_id, $parent, array( $taxonomy ) );
	if ( ! $loop ) {
		return $parent; // No loop.
	}

	// Setting $parent to the given value causes a loop.
	if ( isset( $loop[ $term_id ] ) ) {
		return 0;
	}

	// There's a loop, but it doesn't contain $term_id. Break the loop.
	foreach ( array_keys( $loop ) as $loop_member ) {
		wp_update_term( $loop_member, $taxonomy, array( 'parent' => 0 ) );
	}

	return $parent;
}

常見問題

FAQs
檢視更多 >