我有一个带有插件插入PHP的Wordpress博客,它允许我在帖子中执行PHP代码。我的所有帖子都有图片,文字和链接。有时我需要更改一些链接,我不想每次更新帖子内容,我在数据库中组织了所有内容,我想从这个数据库中动态获取所有链接。这就是我到目前为止的想法:
post_links表:
id | image | link |
1 | http://example.com/1.jpg | http://example.com/somelink1 |
2 | http://example.com/1.jpg | http://example.com/somelink2 |
发布内容:
<p style="text-align: center;"><img class="aligncenter" src="[insert_php]$id=1; $include "image.php";[/insert_php]" />
<p>Some static text here</p>
<a href="[insert_php]$id=1; $include "link.php";[/insert_php]">My dynamic link</a>
image.php
<?php
$con=mysqli_connect("localhost","root","root","wordpress");
$result = mysqli_query($con,"SELECT image FROM post_links WHERE id = '$id' LIMIT 1");
echo $result->fetch_object()->image;
?>
link.php
<?php
$con=mysqli_connect("localhost","root","root","wordpress");
$result = mysqli_query($con,"SELECT link FROM post_links WHERE id = '$id' LIMIT 1");
echo $result->fetch_object()->link;
?>
这样可以正常但我的问题是:每次加载帖子时有2或3个额外的Mysql请求会影响整个站点的性能?有没有更好的方法来存档相同的结果?