根据你的例子
\'example.com/wordpress-how-to/how-to-create-a-child-theme-in-wordpress/\'
搜索=wordpress how to
主题=how-to-create-a-child-theme-in-wordpress/
此代码从搜索词中找到的主题中删除所有单词。
有关更多详细信息,请查看save\\u post中的“避免无限循环”
function modified_slug( $post_id ) {
// If this is a revision, get real post ID
if ( $parent_id = wp_is_post_revision( $post_id ) )
$post_id = $parent_id;
// Get default category ID from options
$defaultcat = get_option( \'default_category\' );
// Check if this post is in default category
if ( in_category( $defaultcat, $post_id ) ) {
// unhook this function so it doesn\'t loop infinitely
remove_action( \'save_post\', \'modified_slug\', 13, 2 );
$post_url = get_permalink( $post_id );
$filtered = array_filter( explode( \'/\', str_replace( home_url(), \'\', $post_url)));
$search = explode(\'-\', array_shift(array_values($filtered)) );
$subject = end($filtered);
$new_slug = trim( str_replace( $search, \'\', $subject ), \'-\' );
// Update post
$my_post = array(
\'ID\' => $post_id,
\'post_name\' => $new_slug
);
// Update the post into the database
wp_update_post( $my_post );
// re-hook this function
add_action( \'save_post\', \'modified_slug\', 13, 2 );
}
}
add_action( \'save_post\', \'modified_slug\', 13, 2 );
- https://codex.wordpress.org/Plugin_API/Action_Reference/save_post
- https://codex.wordpress.org/Function_Reference/wp_is_post_revision
- https://codex.wordpress.org/Function_Reference/wp_update_post