批量修改WordPress旧文章别名
找了段代码实现这个功能:
自己修改了下,改成自己需要的功能了。/** * 修改WordPress旧文章别名为文章ID * 菜鸟笔记整理:https://www.coonote.com */ function Bing_post_name_id(){ query_posts( ‘posts_per_page=-1’ ); while( have_posts() ){ the_post(); $post_id = $GLOBALS[‘post’]->ID; // 生成随机数 $random = mt_rand(1000, 9999); // 获取当前日期 $date = date(‘Ymd’); // 组合字符串:文章ID+随机数+日期 $string_to_hash = $post_ID . $random . $date; // 使用md5加密 $md5 = md5($string_to_hash); // 取md5值的后 8 位 $slug = substr($md5, -8); wp_update_post( array( ‘ID’ => $post_id, 'post_name' => $slug ) ); } wp_reset_query(); } if( $_GET[‘post_name_id’] == ‘ok’ ) add_action( ‘init’, ‘Bing_post_name_id’ );
使用方法:
将上面的代码放在你的主题目录下的functions.php文件最后面,保存。随后,返回你的wp博客首页,访问 https://你的网站域名/?post_name_id=ok
实现所有的文章别名为当前文章ID+随机数,进行md5编码,取md5值的前8位。
仅供参考,文章基数很大保不准有重复,所以建议32位md5取16位。
正文结束
还没有评论,来坐沙发吧。