wordpress链接id连续解决方法

实现原理:利用文章的别名来实现.

老文章批量设置

附上老文章批量修改代码 放到主题目录下的functions.php 里边:

function Bing_post_id_continuous_query(){
	set_time_limit( 0 );
	remove_action( 'publish_post', 'Bing_post_id_continuous' );
	query_posts( 'posts_per_page=-1' );
	$arr = array();
	while( have_posts() ){
		the_post();
		$post_id = $GLOBALS['post']->ID;
		$arr[] = $post_id;
	}
	wp_reset_query();	
	$arr = array_reverse( $arr );
	$i = 1;
	foreach( $arr as $post_id ){
		wp_update_post( array(
			'ID' => $post_id,
			'post_name' => $i++
		) );
	}
}
if( $_GET['post_id_continuous_query'] == 'yes' && current_user_can( 'level_10' ) ) add_action( 'init', 'Bing_post_id_continuous_query' );

新文章自动设置

老文章设置好了,接下来就要给新文章自动设置别名了,只需要把下边的代码添加到主题的 functions.php 即可:

function Bing_post_id_continuous( $id, $post, $update ){
	if( $update || $post->post_status != 'publish' ) return;
	$action = 'save_post_post';
	$func = 'Bing_post_id_continuous';
	remove_action( $action, $func, 10 );
	wp_update_post( array(
		'ID' => $id,
		'post_name' => wp_count_posts()->publish + 1
	));
	add_action( $action, $func, 10, 3 );
}
add_action( 'save_post_post', 'Bing_post_id_continuous', 10, 3 );

添加好后,每发布一篇文章,别名都会自动设置成当前发布文章的数量加 1.

修改固定链接

添加完代码之后,需要在后台的 “设置” → “固定连接” 里设置一下固定连接,把 ID 改成别名,也就是把固定连接里的 post_id 改成postname

ID错乱

如果因为删除、更新或者其它原因导致文章的 “ID” 不连续了,可以使用上边的那个批量设置的代码重新归位。

摘自:https://www.luoxiao123.cn/13478.html