如何在PHP的href链接中插入指定WPSM_Woohelper方法调用代码?
解决方案
首先要明确WPSM_Woohelper::re_show_brand_tax()的运行逻辑:它是直接输出品牌HTML(用echo),还是返回品牌HTML字符串?不同情况处理方式不同,以下是两种场景的操作方法:
场景1:方法直接输出内容(echo)
如果这个方法是直接输出品牌信息,不能直接拼接到字符串里,需要用PHP输出缓冲捕获它的输出,再拼到$text中:
// 开启输出缓冲,捕获方法的输出 ob_start(); WPSM_Woohelper::re_show_brand_tax(); $brand_content = ob_get_clean(); // 将捕获到的品牌内容插入到目标位置(示例插入到".."和a标签之间) $text = '<p>' . substr($content, 0, $pos[$limit]) . "..{$brand_content}<a href='{$product_url}' class='goto_more_offer_tab button'></a></p>";
场景2:方法返回字符串
如果这个方法是返回品牌HTML字符串,直接拼接即可,以下是几个常见插入位置的示例:
插入到".."和a标签之间
$text = '<p>' . substr($content, 0, $pos[$limit]) . ".." . WPSM_Woohelper::re_show_brand_tax() . "<a href='{$product_url}' class='goto_more_offer_tab button'></a></p>";
插入到段落开头
$text = '<p>' . WPSM_Woohelper::re_show_brand_tax() . substr($content, 0, $pos[$limit]) . "..<a href='{$product_url}' class='goto_more_offer_tab button'></a></p>";
插入到a标签之后
$text = '<p>' . substr($content, 0, $pos[$limit]) . "..<a href='{$product_url}' class='goto_more_offer_tab button'></a>" . WPSM_Woohelper::re_show_brand_tax() . '</p>';
注意事项
- 你可以根据实际需求调整品牌内容的插入位置,只要保持PHP字符串拼接的语法正确即可(注意引号转义,原代码里的双引号已经处理了变量解析)。
- 如果不确定方法的运行逻辑,可以先测试:单独执行
WPSM_Woohelper::re_show_brand_tax();,如果页面直接显示品牌信息,就是场景1;如果没有输出但能拿到返回值,就是场景2。
内容的提问来源于stack exchange,提问作者Linux
相关产品推荐
相关产品推荐

