【问题标题】:How to show function result correctly如何正确显示函数结果
【发布时间】:2014-11-07 10:54:43
【问题描述】:

我需要编写可以发送电子邮件或短信的简单脚本。我需要获取函数结果并将其分配给某个变量。例如 $message = message();并在发送短信的脚本中获取 $message。

这是我的代码示例:

function message() { $argsvsq = array( 'date_query' => array(
    array(
        'year' => date( 'Y' ),
        'week' => date( 'W' ),
    ),
),
            'author__in' => array($_GET["sendtoid"]),
            'post_type' => 'ocinky',
            'meta_key' => 'wpcf-date',
            'orderby' => 'meta_value',
            'order' => 'DESC',
            'posts_per_page' => -1
             );

        $looper = new WP_Query( $argsvsq );
        // Start the Loop.
        while ( $looper->have_posts() ) : $looper->the_post(); $urok = types_render_field("urok", array("output"=>"HTML")); echo $urok; endwhile;

        }

这是我需要显示结果的行

$text_sms = iconv('windows-1251', 'utf-8', message() );

请帮助正确获取函数 message() 的结果...非常感谢!

【问题讨论】:

    标签: php wordpress function template-engine


    【解决方案1】:

    iconv 将字符串作为第三个参数。您的 message() 函数不返回任何内容。

    您可以使用输出缓冲来简单地解决这个问题:

    function message() { $argsvsq = array( 'date_query' => array(
        array(
            'year' => date( 'Y' ),
            'week' => date( 'W' ),
        ),
    ),
        'author__in' => array($_GET["sendtoid"]),
        'post_type' => 'ocinky',
        'meta_key' => 'wpcf-date',
        'orderby' => 'meta_value',
        'order' => 'DESC',
        'posts_per_page' => -1
    );
        ob_start();
        $looper = new WP_Query( $argsvsq );
        // Start the Loop.
        while ( $looper->have_posts() ) : $looper->the_post(); 
            $urok = types_render_field("urok", array("output"=>"HTML")); 
            echo $urok; 
        endwhile;
    
        return ob_get_clean();
    }
    

    可能只追加并返回一个字符串,而不是使用输出缓冲:

    function message() { $argsvsq = array( 'date_query' => array(
        array(
            'year' => date( 'Y' ),
            'week' => date( 'W' ),
        ),
    ),
        'author__in' => array($_GET["sendtoid"]),
        'post_type' => 'ocinky',
        'meta_key' => 'wpcf-date',
        'orderby' => 'meta_value',
        'order' => 'DESC',
        'posts_per_page' => -1
    );
        $return = ''
        $looper = new WP_Query( $argsvsq );
        // Start the Loop.
        while ( $looper->have_posts() ) : $looper->the_post(); 
            $urok = types_render_field("urok", array("output"=>"HTML")); 
            $return .= $urok; 
        endwhile;
    
        return $return;
    }
    

    但我不知道所有这些函数调用在做什么(如果它们回显任何内容,您将需要使用第一种方法

    【讨论】:

    • @VladislavPanin 很高兴我能帮上忙
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-07
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    • 2021-11-11
    • 2015-04-21
    相关资源
    最近更新 更多