Php 设置并检索会话值wordpress
我正在使用WooCommerce。 我在page.php上设置了自己的会话,并试图将该会话传递给mini-cart.php,但mini-cart没有收到该会话 php是这样的Php 设置并检索会话值wordpress,php,wordpress,woocommerce,session-variables,Php,Wordpress,Woocommerce,Session Variables,我正在使用WooCommerce。 我在page.php上设置了自己的会话,并试图将该会话传递给mini-cart.php,但mini-cart没有收到该会话 php是这样的 get_header("english"); ?> <div id="primary" class="content-area"> <main id="main" class="site-main" role="main"> <?php // Start the loo
get_header("english"); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
// Start the loop.
// session_start();
while ( have_posts() ) : the_post();
// Include the page content template.
get_template_part( 'template-parts/content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) {
comments_template();
}
// End of the loop.
endwhile;
?>
</main><!-- .site-main -->
<?php get_sidebar( 'content-bottom' ); ?>
</div><!-- .content-area -->
<?php get_sidebar(); ?>
<?php get_footer();
// Adding my own session below
$_SESSION['menu_lang'] = "english";
if (!isset($_SESSION['menu_lang'])) {
echo "no session";
} else {
echo $_SESSION['menu_lang'];
}
如何将会话传递到mini cart?您可以使用以下代码进行操作 在主题的functions.php文件中添加以下代码
function register_session_new(){
if( ! session_id() ) {
session_start();
}
}
add_action('init', 'register_session_new');
$_SESSION['menu_lang'] = "english";
在mini-cart.php文件中或任何您想使用的地方使用这一行
echo $_SESSION['menu_lang'] ;
您可以使用以下代码执行此操作 在主题的functions.php文件中添加以下代码
function register_session_new(){
if( ! session_id() ) {
session_start();
}
}
add_action('init', 'register_session_new');
$_SESSION['menu_lang'] = "english";
在mini-cart.php文件中或任何您想使用的地方使用这一行
echo $_SESSION['menu_lang'] ;
我试过了,但现在mini cart没有收到任何产品,并且出现以下消息致命错误:未捕获错误:在C:\MAMP\htdocs\woocmerce\WP content\themes\twenty1616\page中找不到类“WP\u Session”。php:43堆栈跟踪:#0 C:\MAMP\htdocs\woocmerce\WP includes\template loader.php(74):include()#1c:\MAMP\htdocs\woocommerce\wp blog header.php(19):require#once('C:\\MAMP\\htdocs\\…')#2c:\MAMP\htdocs\woocmerce\index.php(17):require('C:\\MAMP\\htdocs\\…')#3{main}在第43行的C:\MAMP\htdocs\woocmerce\wp content\themes\twentythetic\page.php中抛出您使用的是哪个wordpress版本?我使用的是4.9.6Ok现在我添加了另一种方法检查它。我试过了,我可以在页面上回显$_SESSION['menu_lang',但在迷你购物车上没有…它仍然显示我试过的“无会话”,但是现在mini cart没有收到任何产品,并且出现以下消息致命错误:未捕获错误:在C:\MAMP\htdocs\woocmerce\WP content\themes\twentythetic\page.php:43堆栈跟踪:#0 C:\MAMP\htdocs\woocmerce\WP includes\template loader.php(74):include()#1c:\MAMP\htdocs\woocommerce\wp blog header.php(19):require#once('C:\\MAMP\\htdocs\\…')#2c:\MAMP\htdocs\woocmerce\index.php(17):require('C:\\MAMP\\htdocs\\…')#3{main}在第43行的C:\MAMP\htdocs\woocmerce\wp content\themes\twentythetic\page.php中抛出您使用的是哪个wordpress版本?我使用的是4.9.6Ok,现在我添加了另一种方法检查它。我尝试过了,我可以在页面上回显$\u SESSION['menu_lang',但在迷你购物车上没有…它仍然显示“无会话”