在PHP代码中使用PHP代码?
我在 PHP 中有以下代码:
<?php if (function_exists("insert_audio_player")) {insert_audio_player("[audio:|titles=]"} ?>
它在 WordPress 中将音频播放器渲染到我的页面。我需要在这段代码中调用自定义字段。我的自定义字段代码也是用 PHP 编码的:
<?php print_custom_field('tc_filename'); ?>
类似于:
<?php if (function_exists("insert_audio_player")) {insert_audio_player("[audio:<?php print_custom_field('tc_filename'); ?>|titles=<?php print_custom_field('tc_title'); ?>]"} ?>
如何使用第二个代码块或将第二个代码块与第一个代码块集成?
I have the following code in PHP:
<?php if (function_exists("insert_audio_player")) {insert_audio_player("[audio:|titles=]"} ?>
Which renders an audio player to my page in WordPress. I need to call a custom field inside this code. My custom field code is also coded in PHP:
<?php print_custom_field('tc_filename'); ?>
Something like:
<?php if (function_exists("insert_audio_player")) {insert_audio_player("[audio:<?php print_custom_field('tc_filename'); ?>|titles=<?php print_custom_field('tc_title'); ?>]"} ?>
How can I use or integrate the second block of code with the first?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
sprintf()
编辑:根据您的评论,print_custom_field实际上回显了字段,并且不返回它,如果没有可以使用的返回函数,则需要使用输出缓冲。
您可以使用一个新函数,它调用 print 函数但返回它而不是将其打印到屏幕上:
并使用
You can make this easier to read using
sprintf()
Edit: based on your comment, the print_custom_field actually echo's the field, and doesn't return it, if there is no return function you can use, you need to use Output Buffering.
You can use a new function, which calls the print function but returns it instead of printing it to the screen:
And use
编辑:正如OP所述,
print_custom_field()
函数使用echo
而不是return
,所以这个答案不适用于这种特殊情况。请参阅 @Jacob 的答案以获得更好的解决方案。试试这个:
Edit: As stated by the OP, the
print_custom_field()
function usesecho
rather thanreturn
, so this answer will not work for this particular situation. See @Jacob's answer for a better solution.Try this:
然后 :
then :