Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 获取wordpress中从页面到索引页面的变量_Php_Wordpress - Fatal编程技术网

Php 获取wordpress中从页面到索引页面的变量

Php 获取wordpress中从页面到索引页面的变量,php,wordpress,Php,Wordpress,我在一个名为page pants.php的页面中有一个名为$total的变量, 我需要在index.php页面中回显名为$total的变量。 如何做到这一点?您可以在functions.php中定义和设置它,并在需要的任何地方使用它 例如: add_action('init', 'myStartSession', 1); add_action('wp_logout', 'myEndSession'); add_action('wp_login', 'myEndSession'); functi

我在一个名为
page pants.php
的页面中有一个名为
$total
的变量, 我需要在
index.php
页面中回显名为
$total
的变量。
如何做到这一点?

您可以在functions.php中定义和设置它,并在需要的任何地方使用它

例如:

add_action('init', 'myStartSession', 1);
add_action('wp_logout', 'myEndSession');
add_action('wp_login', 'myEndSession');

function myStartSession() {
    if(!session_id()) {
        session_start();
    }

    $_SESSION['myKey'] = "Some data I need later";
}

function myEndSession() {
    session_destroy ();
}
并在您需要的每个主题文件中显示:

if(isset($_SESSION['myKey'])) {
    $value = $_SESSION['myKey'];
} else {
    $value = '';
}
echo $value;

最好使用一个不过期的瞬态。在
页面pants.php
文件中使用以下代码设置
$total
。我假设每次访问裤子页面时,$总值都在变化

delete_transient( 'pants_total' );
// Save $total as the transient pants_total
set_transient( 'pants_total', $total );
然后,您可以通过以下方式访问WordPress站点中的任意位置的$total

$total = get_transient( 'pants_total' );
echo $total;