调用变量correct并在_content()中显示PHP生成的HTML

调用变量correct并在_content()中显示PHP生成的HTML,php,wordpress,Php,Wordpress,我正在创建一个名为woomps的插件。在主php文件中,我在模板重定向时进行计算 add_action('template_redirect','woomps_loop'); 这个计算x:(这实际上是一个很大的计算) 我想基于一个短代码显示这个变量。因此,在我的主php文件中,我通过以下代码包含了frontend.php function woomps_scripts() { include 'frontend.php'; } add_action('wp_enque

我正在创建一个名为woomps的插件。在主php文件中,我在模板重定向时进行计算

add_action('template_redirect','woomps_loop');
这个计算x:(这实际上是一个很大的计算)

我想基于一个短代码显示这个变量。因此,在我的主php文件中,我通过以下代码包含了frontend.php

    function woomps_scripts() {
    include 'frontend.php'; 
}
    add_action('wp_enqueue_scripts','woomps_scripts');
其中frontend.php包含以下代码

    function woomps_subscription_slider (){
    //How do I call the variable from woomps_loop() without running all the code again.
    echo "<div>\n";
    echo "<p>\n"; 
    echo $x; 
    echo "</p>\n";
    echo "</div>\n";
}
add_shortcode("woomps-subscription-slider", "woomps_subscription_slider");
功能woomps\u订阅\u滑块(){
//如何在不再次运行所有代码的情况下从woomps_loop()调用变量。
回音“\n”;
回声“\n”;
echo$x;
回声“

\n”; 回音“\n”; } 添加快捷代码(“woomps订阅滑块”、“woomps订阅滑块”);
短代码被添加到我的一个页面中,因此它会显示出来,但是


  • 如何调用
    $x
    变量形式
    woomps\u loop()
    而不再次运行代码?
  • 将位于
    内容()中生成的所有其他内容之前。为什么会这样?
  • 1) 将其存储为全局变量。因此,将
    woomps\u循环
    函数更改为

    global $x;
    
    function woomps_loop() {
        global $x;
        $x = 10;
        return $x
    }
    
    (PS您现在可能不需要在此处返回
    $x
    ) 然后在快捷码函数中定义
    $x

    function woomps_subscription_slider (){
        global $x;
        echo "<div>\n";
        //... rest of it carries on
    
    功能woomps\u订阅\u滑块(){
    全球x美元;
    回音“\n”;
    //…其余部分继续进行
    
    2) 因为您正在
    echo
    ing。请返回值

    global $x; //Had to define it global before setting it, i dident understand this before @cameronjonesweb exmample.
    $x = $total_qty;
    
    然后像这样引用它:

    function woomps_subscription_slider ($x){
    global $x_subs;
    $content = <<<EOD
    <div>
    <p>
    {$x_subs}
    </p>
    </div>
    EOD;
    
    return $content;
    } //end woomps_subscription_slider
    
    功能woomps\u订阅\u滑块($x){
    全球$x_潜艇;
    
    $content=用$x替换掉$x_subs。也许我不应该回答我自己的问题:| stackoverflow新手。
    function woomps_subscription_slider ($x){
    global $x_subs;
    $content = <<<EOD
    <div>
    <p>
    {$x_subs}
    </p>
    </div>
    EOD;
    
    return $content;
    } //end woomps_subscription_slider