gpt4 book ai didi

php - 如何在wordpress函数中跳过具有特定类别的图像

转载 作者:太空宇宙 更新时间:2023-11-04 15:29:19 25 4
gpt4 key购买 nike

我的主题功能页面中有以下功能。基本上它所做的是在帖子页面中查找任何图像并使用 css 添加一些 span 以动态创建一个 pinterest 按钮。

function insert_pinterest($content) {
global $post;

$posturl = urlencode(get_permalink()); //Get the post URL
$pinspan = '<span class="pinterest-button">';
$pinurlNew = '<a href="#" onclick="window.open(&quot;http://pinterest.com/pin/create/button/?url='.$posturl.'&amp;media=';

$pindescription = '&amp;description='.urlencode(get_the_title());
$options = '&quot;,&quot;Pinterest&quot;,&quot;scrollbars=no,menubar=no,width=600,height=380,resizable=yes,toolbar=no,location=no,status=no';
$pinfinish = '&quot;);return false;" class="pin-it"></a>';
$pinend = '</span>';
$pattern = '/<img(.*?)src="(.*?).(bmp|gif|jpeg|jpg|png)"(.*?) \/>/i';
$replacement = $pinspan.$pinurlNew.'$2.$3'.$pindescription.$options.$pinfinish.'<img$1src="$2.$3" $4 />'.$pinend;
$content = preg_replace( $pattern, $replacement, $content );

//Fix the link problem
$newpattern = '/<a(.*?)><span class="pinterest-button"><a(.*?)><\/a><img(.*?)\/><\/span><\/a>/i';
$replacement = '<span class="pinterest-button"><a$2></a><a$1><img$3\/></a></span>';

$content = preg_replace( $newpattern, $replacement, $content );
return $content;
}
add_filter( 'the_content', 'insert_pinterest' );

它做的一切都很好。但是有没有办法让它跳过带有特定类名的图像,比如“noPin”?

最佳答案

我会使用 preg_replace_callback 来检查匹配的图像是否包含 noPin。

function skipNoPin($matches){
if ( strpos($matches[0], "noPin") === false){
return $pinspan.$pinurlNew.'$matches[2].$matches[3]'.$pindescription.$options.$pinfinish.'<img$1src="$2.$3" $4 />'.$pinend;
} else {
return $matches[0]

$content = preg_replace_callback(
$pattern,
skipNoPin,
$content );

另一个图像属性可能包含 noPin,如果您担心这种极端情况,只需在 if 语句中进行更具体的测试即可。

关于php - 如何在wordpress函数中跳过具有特定类别的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13614282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com