替换 WordPress 内容中的字符串数组

发布于 2024-08-26 22:26:55 字数 308 浏览 3 评论 0原文

我正在开发一个音乐博客,提供从 0.0 到 10 的评论分数。由于作者已经开发了在内容中输入分数的系统,因此我正在尝试找到一种方法来更好地强调它们。

示例:

"Score: 6.4" 

返回类似

<div class="score">6.4</div>

Is there way to do this in a array 将每个分数可能性放在 function.php 或 single.php 页面上。随便什么都比较干净。

I'm working on a music blog that provides review scores ranging from 0.0 - 10. Since the authors already developed their system of typing in the score in the content, I'm trying figure out a way to emphasis them better.

Example:

"Score: 6.4" 

returns something like

<div class="score">6.4</div>

Is there way to do this in an array to put every score possibility down within function.php or on the single.php page. Whatever is cleaner.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

や三分注定 2024-09-02 22:26:55

您可以对内容进行正则表达式搜索/替换。然后,您可以通过创建插件并使用 wp add_filter 函数将其连接到 Wordpress。

function expand_scores($content) {
 return preg_replace('/(score):\s*([\d.]+)/ims', '<div class="score">$1: $2</div>', $content);
}
add_filter('the_content', 'expand_scores');

expand_scoresadd_filter 调用都会进入您的插件文件。 the_content 挂钩将 Expand_scores 函数应用于打印前从数据库检索的所有发布数据。

You can do a regular expression search/replace for the content. Then you can hook it into Wordpress by creating a plugin and using the wp add_filter function.

function expand_scores($content) {
 return preg_replace('/(score):\s*([\d.]+)/ims', '<div class="score">$1: $2</div>', $content);
}
add_filter('the_content', 'expand_scores');

Both the expand_scores and the add_filter call go into your plugin file. The the_content hook applies the expand_scores function to all post data retrieved from the database before printing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文