Javascript 在外部.js文件中使用全局php数组
我正在为wordpress开发一个主题框架,我遇到了一个小问题 我已经定义了一个$globals数组,它存储当前注册的帖子类型以及一些用于主题布局选项页面的特殊页面Javascript 在外部.js文件中使用全局php数组,javascript,php,jquery,wordpress,Javascript,Php,Jquery,Wordpress,我正在为wordpress开发一个主题框架,我遇到了一个小问题 我已经定义了一个$globals数组,它存储当前注册的帖子类型以及一些用于主题布局选项页面的特殊页面 $GLOBALS['gc_frame_postTypes'] = array('homepage', 'blog'); $post_types = get_post_types( '', 'names' ); foreach ( $post_types as $post_type ) {
$GLOBALS['gc_frame_postTypes'] = array('homepage', 'blog');
$post_types = get_post_types( '', 'names' );
foreach ( $post_types as $post_type ) {
if ($post_type != "revision" && $post_type != "nav_menu_item" && $post_type != "attachment")
$GLOBALS['gc_frame_postTypes'][] = $post_type;
}
我有我的选项页面工作,它正确地创建了所有正确的选项
我遇到的问题是,当我尝试应用javascript来改进页面的外观和功能时
下面是实现我想要的功能的.js代码。它根据选定的布局显示和隐藏字段
jQuery(document).ready(function() {
// keeps the new sidebar text block blank
jQuery("#gc_frame_sidebar_name_new").val("")
// Homepage layout Function call
jQuery.fn.layoutSidebars('homepage');
// page layout Function call
jQuery.fn.layoutSidebars('page');
// blog layout Function call
jQuery.fn.layoutSidebars('blog');
// post layout Function call
jQuery.fn.layoutSidebars('post');
// Homepage layout Function call on change
jQuery('#gc_frame_homepage_layout').change(function(){
jQuery.fn.layoutSidebars('homepage');
});
// page layout Function call on change
jQuery('#gc_frame_page_layout').change(function(){
jQuery.fn.layoutSidebars('page');
});
// blog layout Function call on change
jQuery('#gc_frame_blog_layout').change(function(){
jQuery.fn.layoutSidebars('blog');
});
// post layout Function call on change
jQuery('#gc_frame_post_layout').change(function(){
jQuery.fn.layoutSidebars('post');
});
});
jQuery.fn.layoutSidebars = function(pageType){
pageLayout = jQuery("#gc_frame_" +pageType+ "_layout").val();
if (pageLayout == 'fullwidth'){
jQuery("label[for^='gc_frame_" +pageType+ "_left_sidebar_']").closest('tr').hide();
jQuery("#gc_frame_" +pageType+ "_left_sidebar_count").val(0);
jQuery("label[for^='gc_frame_" +pageType+ "_right_sidebar_']").closest('tr').hide();
jQuery("#gc_frame_" +pageType+ "_right_sidebar_count").val(0);
} else if (pageLayout == 'SB-L') {
jQuery("label[for='gc_frame_" +pageType+ "_left_sidebar_count']").closest('tr').show();
jQuery("label[for^='gc_frame_" +pageType+ "_right_sidebar_']").closest('tr').hide();
jQuery("#gc_frame_" +pageType+ "_right_sidebar_count").val(0);
} else if (pageLayout == 'SB-R') {
jQuery("label[for^='gc_frame_" +pageType+ "_left_sidebar_']").closest('tr').hide();
jQuery("#gc_frame_" +pageType+ "_left_sidebar_count").val(0);
jQuery("label[for='gc_frame_" +pageType+ "_right_sidebar_count']").closest('tr').show();
} else if (pageLayout == 'SB-LR') {
jQuery("label[for='gc_frame_" +pageType+ "_left_sidebar_count']").closest('tr').show();
jQuery("label[for='gc_frame_" +pageType+ "_right_sidebar_count']").closest('tr').show();
}
}
我想做的是将上面的全局数组以某种方式放入javascript中,这样,如果添加了新的自定义post类型,它们也会受到javascript的影响
我目前有两个文件中的php和javascript,我不知道如何从php文件到javascript中获取信息
任何帮助都会很好您应该使用
$\u SESSION
变量在页面中共享信息
在第一页中,在加载javascript文件之前:
session_start();
$_SESSION['globalVar'] = Value;
在外部javascript文件(扩展名为.php)中,您可以得到如下结果:
<?php session_start(); //at the top of your JavaScript?>
...
var globalVar = '<?php echo $_SESSION['globalVar']; ?>'
...
...
变量globalVar=''
...
您可以使用wp\u localize\u script()
这里有一个很好的资源:可以将jQuery关键字改为$。i、 e$('#gc#frame_post_layout')。更改而不是jQuery('#gc#frame_post_layout')。更改将其直接打印到javascript变量中,使用ajax请求,json编码并将其打印到数据属性中,etcI添加了上面的行,用我的全局数组替换“Value”,并向.js.php文件添加了一个警报,我返回并取消定义!在第二个文件中,您需要将
放在文件顶部以加载会话。好的,我尝试了,但仍然得到一个空白。我还尝试了var globalVar=''行,现在我的警报中有一个空值。您需要将会话_start()放在这两个文件中。现在我们可以从我的警报中获得[“主页”、“博客”、“帖子”、“页面”]。现在,我只需要将其正确格式化,以便将其用作javascript数组