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

推荐订阅源

S
Secure Thoughts
Security Latest
Security Latest
Simon Willison's Weblog
Simon Willison's Weblog
O
OpenAI News
GbyAI
GbyAI
L
LINUX DO - 最新话题
A
Arctic Wolf
T
Tor Project blog
G
GRAHAM CLULEY
I
InfoQ
博客园_首页
IT之家
IT之家
The Register - Security
The Register - Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
The GitHub Blog
The GitHub Blog
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
K
Kaspersky official blog
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
U
Unit 42
PCI Perspectives
PCI Perspectives
量子位
P
Palo Alto Networks Blog
S
Securelist
T
Troy Hunt's Blog
博客园 - 【当耐特】
Recorded Future
Recorded Future
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
Engineering at Meta
Engineering at Meta
T
The Blog of Author Tim Ferriss
博客园 - 聂微东
罗磊的独立博客
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
B
Blog RSS Feed
NISL@THU
NISL@THU
C
Cisco Blogs
T
Threatpost
有赞技术团队
有赞技术团队
Forbes - Security
Forbes - Security
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
T
The Exploit Database - CXSecurity.com
Cloudbric
Cloudbric
Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
C
Cyber Attacks, Cyber Crime and Cyber Security

周良 Len

给朋友们的 AI 使用建议 内部分享 PPT 与文档:浅析 SEO 排名优化技巧 内部分享 PPT 与文档:浅析 SEO 排名优化技巧 使用 Cloudflare Workers 为 Bento.me 设置自定义域名 使用 Cloudflare Workers 为 Bento.me 设置自定义域名 Crypto 阅读列表 Crypto 阅读列表 Atomicals 挖矿程序部署与钱包管理 Atomicals 挖矿程序部署与钱包管理 Starknet 早期社区成员计划申请指南 Starknet 早期社区成员计划申请指南 如何绕过烦人的 hCaptcha 验证 如何绕过烦人的 hCaptcha 验证 白话比特币原理及运作机制 白话比特币原理及运作机制 什么是产品人? 什么是产品人? 独立博客三大乐趣:主题、域名、建站程序 独立博客三大乐趣:主题、域名、建站程序 麻将 麻将 2020.10.15 杂记:有钱人的定义、iOS 工作日智能闹钟、iPhone 12 2020.10.15 杂记:有钱人的定义、iOS 工作日智能闹钟、iPhone 12 公司五行论与组织六元拟人指标 公司五行论与组织六元拟人指标 修真世界 修真世界 玩法分析:让人停不下来的抽奖活动 玩法分析:让人停不下来的抽奖活动 Chrome 网页中同时查找多个关键词内容的方法 Chrome 网页中同时查找多个关键词内容的方法 解决 WordPress 错误页面无法正确识别为 404 页面问题 解决 WordPress 错误页面无法正确识别为 404 页面问题 WordPress 文章内容自动为 a、p 等标签插入指定 Class 方法 解决 WordPress 长时间处于「正在执行例行维护,请一分钟后回来」状态问题 解决 WordPress 长时间处于「正在执行例行维护,请一分钟后回来」状态问题 WordPress 更换域名后完美 301 跳转方案 WordPress 更换域名后完美 301 跳转方案 2018 年看过的书
WordPress 文章内容自动为 a、p 等标签插入指定 Class 方法
周良 Len · 2020-07-31 · via 周良 Len

上周在写个主题,因为引用了开源的 CSS 框架,各类前端标签添加 Class 就行,比较简单。但写到文章内容的时候就有问题了,内容都是 WP 自己输出的,我控制不了所使用的 Class。

问了一下 @desin(我爱水煮鱼博客作者) 有没有不写 js 去添加 class 的方式,desin 提到可以看一下 content 的文档。研究了一下,问题解决。

代码丢上来,仅供大家参考,复制下面的代码丢到主题 function.php 文件的 <?php 下面一行即可 :

# 文章内 img 标签自动添加 img-res Class
function img_paragraph($content){
    return preg_replace('/<img([^>]+)?>/', '<img$1 class="img-res">', $content);
}
add_filter('the_content', 'img_paragraph');

同理,你要控制 p 标签的话,代码如下:

# 文章内 p 标签自动添加 mt1em mb1em lh-180 三个 Class
function p_paragraph($content){
    return preg_replace('/<p([^>]+)?>/', '<p$1 class="mt1em mb1em lh-180">', $content);
}
add_filter('the_content', 'p_paragraph');