wp_set_unique_slug_on_create_template_part

函式
wp_set_unique_slug_on_create_template_part ( $post_id )

wp_set_unique_slug_on_create_template_part: 這個函式為新建立的模板部分設定一個唯一的lug。

在建立自動起草的模板部分時,設定一個自定義lug。

這隻適用於由普通WP編輯器建立的自動草稿。如果這個頁面要被刪除,這將是不必要的。

function wp_set_unique_slug_on_create_template_part( $post_id ) {
	$post = get_post( $post_id );
	if ( 'auto-draft' !== $post->post_status ) {
		return;
	}

	if ( ! $post->post_name ) {
		wp_update_post(
			array(
				'ID'        => $post_id,
				'post_name' => 'custom_slug_' . uniqid(),
			)
		);
	}

	$terms = get_the_terms( $post_id, 'wp_theme' );
	if ( ! is_array( $terms ) || ! count( $terms ) ) {
		wp_set_post_terms( $post_id, wp_get_theme()->get_stylesheet(), 'wp_theme' );
	}
}

常見問題

FAQs
檢視更多 >