在WordPress或PHP中的文件之间传递变量
在inc/contact-form-processor.php文件中,我设置了在WordPress或PHP中的文件之间传递变量,php,wordpress,Php,Wordpress,在inc/contact-form-processor.php文件中,我设置了 $form_complete = FALSE; 在我的template-contact.php文件中 <?php /* Template Name: Contact Page */ ?> <?php require_once 'inc/contact-form-processor.php'; ?> <?php get_header(); $SidebarPosition
$form_complete = FALSE;
在我的template-contact.php文件中
<?php
/*
Template Name: Contact Page
*/
?>
<?php require_once 'inc/contact-form-processor.php'; ?>
<?php get_header();
$SidebarPosition = sidebar_position()[0];
$IndivSidebarPosition = sidebar_position()[1];
$DefaultSidebarPosition = sidebar_position()[2];
?>
<div class="container">
<div class="row">
<?php if ( $SidebarPosition == 'left' ) {
get_template_part( 'layouts/contact/left', 'sidebar' );
}
if ( $SidebarPosition == 'right' ) {
get_template_part( 'layouts/contact/right', 'sidebar' );
}
if ( ( $IndivSidebarPosition == 'none' ) || ( $IndivSidebarPosition == 'default' and $DefaultSidebarPosition == 'none' ) ) {
get_template_part( 'layouts/contact/no', 'sidebar' );
}
?>
<?php echo $IndivSidebarPosition = sidebar_position()[1]; ?>
</div>
</div>
<?php get_footer(); ?>
它根据条件显示联系人表单
<div id="contact_form">
<?php if($form_complete === FALSE) { ?>
<form>
.. Form ...
</form>
<?php } ?>
</div>
当前脚本中可用的所有变量都将在该脚本中可用
模板文件现在也是
但我不确定代码放在哪个文件中,以及应该引用哪个文件。您的问题与范围有关。这段代码没有魔术
include(locate_template('your-template-name.php'));
定位模板只返回文件名(它在主题中查找适当的文件,通常用于允许子主题覆盖)。与您相关的是,include
将文件加载到调用它的函数/行的相同范围内
让我们看看这个:
$a= 'im outside scope';
$b = 'im outside scope but get passed into the function so i can be used';
function sample($var){
$c= 'in scope';
$d= $var;
include 'template.php';
}
sample($b);
Template.php
<?php
echo $a; // ''
echo $b; // ''
echo $c; // 'in scope'
echo $d; // 'im outside scope but get passed into the function so i can be used'
$a= 'im outside scope';
$b = 'im outside scope but get passed into the function so i can be used';
function sample($var){
$c= 'in scope';
$d= $var;
include 'template.php';
}
sample($b);
<?php
echo $a; // ''
echo $b; // ''
echo $c; // 'in scope'
echo $d; // 'im outside scope but get passed into the function so i can be used'