Home 教程 post_name自动设置为文章id

post_name自动设置为文章id

今天一位客户,固定链接设置的/%postname%.html,但是想要前台展示为  27.html 这样的数字链接,所以一直以来,是手动来修改固定链接的。。。

随着文章越来越多,这样修改变得好麻烦 ,决定改成自动的,刚开始以为直接修改后台设置固定链接为/%post_id%.html就好了,但是发现文章错位了,还有的变成了404,于是查看相关的钩子,写出了如下解决问题的代码。

在主题的functions.php文件或者插件的入口文件中加入如下代码:

add_action(‘get_sample_permalink’, ‘set_post_name’, 10, 5);
function set_post_name( $permalink, $post_ID, $title, $name, $post){
if($post->post_name != $post_ID){
$post->post_name = $post_ID;
wp_update_post($post);
$permalink[1] = $post_ID;
}
return $permalink;
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Recent Posts