在PHP代码中使用PHP代码?

发布于 2024-10-21 16:43:05 字数 569 浏览 5 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

森末i 2024-10-28 16:43:05
<?php 
if (function_exists("insert_audio_player")) {
    insert_audio_player("[audio:".print_custom_field('tc_filename')."|titles=".print_custom_field('tc_title')."]"
} ?>

您可以使用 sprintf()

<?php 
if (function_exists("insert_audio_player")) {
    insert_audio_player(sprintf(
        '[audio:%s|titles=%s]', 
        print_custom_field('tc_filename'), 
        print_custom_field('tc_title')
    ));
} 
?>    

编辑:根据您的评论,print_custom_field实际上回显了字段,并且不返回它,如果没有可以使用的返回函数,则需要使用输出缓冲

您可以使用一个新函数,它调用 print 函数但返回它而不是将其打印到屏幕上:

function get_custom_field($field) {
    ob_start();
    print_custom_field($field);
    return ob_get_clean();
}

并使用

<?php 
if (function_exists("insert_audio_player")) {
    insert_audio_player(sprintf(
        '[audio:%s|titles=%s]', 
        get_custom_field('tc_filename'), 
        get_custom_field('tc_title')
    ));
} 
?> 
<?php 
if (function_exists("insert_audio_player")) {
    insert_audio_player("[audio:".print_custom_field('tc_filename')."|titles=".print_custom_field('tc_title')."]"
} ?>

You can make this easier to read using sprintf()

<?php 
if (function_exists("insert_audio_player")) {
    insert_audio_player(sprintf(
        '[audio:%s|titles=%s]', 
        print_custom_field('tc_filename'), 
        print_custom_field('tc_title')
    ));
} 
?>    

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:

function get_custom_field($field) {
    ob_start();
    print_custom_field($field);
    return ob_get_clean();
}

And use

<?php 
if (function_exists("insert_audio_player")) {
    insert_audio_player(sprintf(
        '[audio:%s|titles=%s]', 
        get_custom_field('tc_filename'), 
        get_custom_field('tc_title')
    ));
} 
?> 
一指流沙 2024-10-28 16:43:05

编辑:正如OP所述,print_custom_field()函数使用echo而不是return,所以这个答案不适用于这种特殊情况。请参阅 @Jacob 的答案以获得更好的解决方案。

试试这个:

<?php
    if (function_exists("insert_audio_player")) {
        insert_audio_player(
            "[audio:" . print_custom_field('tc_filename') .
            "|titles=" . print_custom_field('tc_title') . "]"
        );
    }
?>

Edit: As stated by the OP, the print_custom_field() function uses echo rather than return, so this answer will not work for this particular situation. See @Jacob's answer for a better solution.

Try this:

<?php
    if (function_exists("insert_audio_player")) {
        insert_audio_player(
            "[audio:" . print_custom_field('tc_filename') .
            "|titles=" . print_custom_field('tc_title') . "]"
        );
    }
?>
冧九 2024-10-28 16:43:05
<?php if (function_exists("insert_audio_player")) {insert_audio_player("[audio:".function2()."|titles=".function3()."]"} ?>

然后 :

function function2()
{
print_custom_field('tc_filename');
}
function function3()
{
print_custom_field('tc_title');
}
<?php if (function_exists("insert_audio_player")) {insert_audio_player("[audio:".function2()."|titles=".function3()."]"} ?>

then :

function function2()
{
print_custom_field('tc_filename');
}
function function3()
{
print_custom_field('tc_title');
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文