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编辑器创建的自动草稿。如果这个页面要被删除,这将是不必要的。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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' );
}
}
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' ); } }
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
查看更多 >