使用 PHP 动态修改 Wordpress post 附件 img URL
Dynamically modify Wordpress post attachment img URLs using PHP
是否可以修改当前post的图像附件的URL,以便浏览器在URL中接收修改后的html img 标签?
我不想更新图像附件属性。
我正在寻找与 post 内容相同的灵活性,因为它可以读入,然后修改 return the_content [=30] =].
我知道 wp_get_attachment_image_src()
和 the_post_thumbnail_url()
是获取附件 URL 的方法。但是我找不到任何方法来 return 为当前的 post 修改 URL。
一种可能性是use output buffering to catch the final HTML,使用ob_start()
等
但是有没有一些更有针对性的解析post图片附件的方法呢?
我已经使用输出缓冲回答了我自己的问题。请注意,我使用的是 Genesis 主题,因此可以访问其他挂钩,例如循环之后和页脚之前的 genesis_after_content,因此完整的 html 内容已经准备就绪。这允许访问特色图像 URL(不可能从 get_the_content)加上来自特色 page/post 小部件的 any/all 图像 URL,等等......无论你在呈现的内容中看到什么。
<?php
//at start of php script set output buffering
ob_start();
session_start();
//script does stuff
if (isset($_SESSION['some_variable_is_set'])) {
add_action( 'genesis_after_content', 'my_replace_image_urls');
//used genesis_after_content hook as full html content is now prepared
}
function my_replace_image_urls() {
$full_content = ob_get_clean();
//modify $full_content as needed
echo $full_content;
}
是否可以修改当前post的图像附件的URL,以便浏览器在URL中接收修改后的html img 标签?
我不想更新图像附件属性。
我正在寻找与 post 内容相同的灵活性,因为它可以读入,然后修改 return the_content [=30] =].
我知道 wp_get_attachment_image_src()
和 the_post_thumbnail_url()
是获取附件 URL 的方法。但是我找不到任何方法来 return 为当前的 post 修改 URL。
一种可能性是use output buffering to catch the final HTML,使用ob_start()
等
但是有没有一些更有针对性的解析post图片附件的方法呢?
我已经使用输出缓冲回答了我自己的问题。请注意,我使用的是 Genesis 主题,因此可以访问其他挂钩,例如循环之后和页脚之前的 genesis_after_content,因此完整的 html 内容已经准备就绪。这允许访问特色图像 URL(不可能从 get_the_content)加上来自特色 page/post 小部件的 any/all 图像 URL,等等......无论你在呈现的内容中看到什么。
<?php
//at start of php script set output buffering
ob_start();
session_start();
//script does stuff
if (isset($_SESSION['some_variable_is_set'])) {
add_action( 'genesis_after_content', 'my_replace_image_urls');
//used genesis_after_content hook as full html content is now prepared
}
function my_replace_image_urls() {
$full_content = ob_get_clean();
//modify $full_content as needed
echo $full_content;
}