惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
Microsoft Security Blog
Microsoft Security Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
V
Visual Studio Blog
宝玉的分享
宝玉的分享
IT之家
IT之家
人人都是产品经理
人人都是产品经理
T
The Blog of Author Tim Ferriss
I
InfoQ
B
Blog RSS Feed
T
Threatpost
博客园_首页
M
MIT News - Artificial intelligence
Spread Privacy
Spread Privacy
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Know Your Adversary
Know Your Adversary
U
Unit 42
Engineering at Meta
Engineering at Meta
C
Cyber Attacks, Cyber Crime and Cyber Security
月光博客
月光博客
Scott Helme
Scott Helme
T
Tor Project blog
有赞技术团队
有赞技术团队
AWS News Blog
AWS News Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Last Week in AI
Last Week in AI
S
Schneier on Security
Vercel News
Vercel News
博客园 - Franky
C
Cybersecurity and Infrastructure Security Agency CISA
L
LINUX DO - 热门话题
NISL@THU
NISL@THU
L
LangChain Blog
爱范儿
爱范儿
Google DeepMind News
Google DeepMind News
The GitHub Blog
The GitHub Blog
雷峰网
雷峰网
Latest news
Latest news
C
CXSECURITY Database RSS Feed - CXSecurity.com
Hugging Face - Blog
Hugging Face - Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
G
GRAHAM CLULEY
S
Security Affairs
A
About on SuperTechFans
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
大猫的无限游戏
大猫的无限游戏
W
WeLiveSecurity
Cisco Talos Blog
Cisco Talos Blog
罗磊的独立博客

一纸忘忧

ONE DAY 0310 重启 One Day 计划 成立一周年!开源的本土化中文文档知识库 - 一纸忘忧 也许世界并不美好,但也没有那么糟糕 - 一纸忘忧 如何在团队项目中白嫖 Vercel 的免费服务 - 一纸忘忧 梅开二度,土区 iCloud 再次涨价 - 一纸忘忧 解决 macOS 导入宝塔面板 SSL 证书失败 阿里云高校计划免费领取五年 ECS 服务器 - 一纸忘忧 HTML Video 在 Vue 中消失的 muted 属性 konva.js 阻止 click 事件冒泡到父元素 - 一纸忘忧
WordPress 设置文章 post_name 与 ID 同步
博主: 一纸忘忧 · 2023-07-20 · via 一纸忘忧

前言

我一直有个强迫症,看着 WordPress 的文章中有个字段 post_name 如果不设置的话,就会默认使用标题 title 作为 post_name,导致使用 post_name 作为文章 URL 的时候会出现中文,看着很不协调。

然而我又不想手动去设置 post_name,于是乎想着能不能将 post_name 默认设置成与 ID 相同。

批量替换现有数据

首先将已经发布的文章给全部替换掉,直接用 SQL 语句

UPDATE wp_posts SET post_name = ID;

这里的 wp_posts 是 WordPress 数据库中的文章表。

发布文章自动修改

要在 WordPress 中每次发布或更新文章时,默认将 post_name 设置为与 ID 相同,可以通过添加自定义功能来实现此功能。

打开 WordPress 主题文件夹中的 functions.php 文件,在文件的末尾添加以下代码:

function set_postname_as_id( $post_id ) {
    $post = get_post( $post_id );
    $post_name = $post->post_name;
    $post_id = $post->ID;

    if ( $post_name != $post_id ) {
        $args = array(
            'ID' => $post_id,
            'post_name' => $post_id,
        );
        wp_update_post( $args );
    }
}
add_action( 'save_post', 'set_postname_as_id' );

保存并上传 functions.php 文件到您的 WordPress 主题文件夹。

每当发布或更新文章时,WordPress 将检查文章的 post_name 是否与 ID 相同。如果不同,它将自动更新 post_name 为 ID。请注意,这将影响所有类型的文章,包括页面和自定义文章类型。如果只想应用于特定类型的文章,可以在 set_postname_as_id 函数内添加适当的条件检查。

比如我用的是 B2 主题,我想替换所有的文章类型,除了 page 页面,可以改成下面这样:

function set_postname_as_id( $post_id ) {
    // 获取文章对象
    $post = get_post( $post_id );

    // 获取当前文章的 post_name 和 ID
    $post_name = $post->post_name;
    $post_id = $post->ID;

    // 检查文章类型是否为 page,如果是则返回
    if ( $post->post_type == 'page' ) {
        return;
    }

    // 检查 post_name 是否与 ID 不同
    if ( $post_name != $post_id ) {
        // 准备要更新的文章参数
        $args = array(
            'ID' => $post_id,
            'post_name' => $post_id,
        );

        // 更新文章的 post_name 为 ID
        wp_update_post( $args );
    }
}

// 在保存文章时触发自定义功能
add_action( 'save_post', 'set_postname_as_id' );

赞赏作者

如果觉得我的文章对你有用,请随意赞赏