Php 该函数仅处理最后一个寻求的模式。怎么了? /** *ACF的快速链接 */ 函数替换_文本($content){ $quick\u links=get\u字段('quick\u links','option'); if($quick\u links&&is\u singular('post')){ foreach($quick_链接为$item){ $word=$item['word\u quick\u links']; $link=$item['link\u quick\u links']; $preg_replace=preg_replace('/\b'.preg_quote($word,'/'))。\b/,'','$content,1); } 返回$preg_replace; }否则{ 返回$content; } } 添加过滤器(“内容”,“替换文本”,20);
在preg_replace()函数中,最后一个参数是limit—每个主题行中每个模板的最大替换次数。默认情况下,它等于-1(无限制)Php 该函数仅处理最后一个寻求的模式。怎么了? /** *ACF的快速链接 */ 函数替换_文本($content){ $quick\u links=get\u字段('quick\u links','option'); if($quick\u links&&is\u singular('post')){ foreach($quick_链接为$item){ $word=$item['word\u quick\u links']; $link=$item['link\u quick\u links']; $preg_replace=preg_replace('/\b'.preg_quote($word,'/'))。\b/,'','$content,1); } 返回$preg_replace; }否则{ 返回$content; } } 添加过滤器(“内容”,“替换文本”,20);,php,wordpress,advanced-custom-fields,Php,Wordpress,Advanced Custom Fields,在preg_replace()函数中,最后一个参数是limit—每个主题行中每个模板的最大替换次数。默认情况下,它等于-1(无限制) 我的错误是什么,为什么函数只处理最后一个模板?在替换内容中文本的内部循环中,您总是从原始文本($content)开始,然后返回一个新字符串($preg\u replace) 然后返回此值(您可以始终返回$content /** * Quick Links for ACF */ function replace_text($content) { $qui
我的错误是什么,为什么函数只处理最后一个模板?在替换内容中文本的内部循环中,您总是从原始文本(
$content
)开始,然后返回一个新字符串($preg\u replace
)
然后返回此值(您可以始终返回$content
/**
* Quick Links for ACF
*/
function replace_text($content) {
$quick_links = get_field('quick_links', 'option');
if($quick_links && is_singular('post')) {
foreach($quick_links as $item) {
$word = $item['word_quick_links'];
$link = $item['link_quick_links'];
$preg_replace = preg_replace('/\b'.preg_quote($word, '/').'\b/', '<a href="' . $link . '" target="_blank">' . $word . '</a>', $content, 1);
}
return $preg_replace;
} else {
return $content;
}
}
add_filter('the_content', 'replace_text', 20 );
$preg_replace = preg_replace('/\b'.preg_quote($word, '/').'\b/', '<a href="' . $link . '" target="_blank">' . $word . '</a>', $content, 1);
$content = preg_replace('/\b'.preg_quote($word, '/').'\b/',
'<a href="' . $link . '" target="_blank">' . $word . '</a>',
$content, 1);
在替换内容中文本的内部循环中,始终从原始文本(
$content
)开始,然后返回新字符串($preg\u replace
)
然后返回此值(您可以始终返回$content
/**
* Quick Links for ACF
*/
function replace_text($content) {
$quick_links = get_field('quick_links', 'option');
if($quick_links && is_singular('post')) {
foreach($quick_links as $item) {
$word = $item['word_quick_links'];
$link = $item['link_quick_links'];
$preg_replace = preg_replace('/\b'.preg_quote($word, '/').'\b/', '<a href="' . $link . '" target="_blank">' . $word . '</a>', $content, 1);
}
return $preg_replace;
} else {
return $content;
}
}
add_filter('the_content', 'replace_text', 20 );
$preg_replace = preg_replace('/\b'.preg_quote($word, '/').'\b/', '<a href="' . $link . '" target="_blank">' . $word . '</a>', $content, 1);
$content = preg_replace('/\b'.preg_quote($word, '/').'\b/',
'<a href="' . $link . '" target="_blank">' . $word . '</a>',
$content, 1);
非常感谢。你帮了很多忙。谢谢你的详细解释。非常感谢。你帮了很多忙。谢谢你的详细解释。