dongwen7380 2013-12-16 19:31
浏览 50
已采纳

在外部.js文件中使用全局php数组

I am working on a theme framework for wordpress, and I have run into a small problem.

I have a defined a $globals array that stores the current registered post types plus a few special pages for the theme layout options page.

$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;
        }

I have this working for my options page, and it correctly creates all the correct options.

The issue I am having is when I try to apply javascript to improve the look and function of the page.

Here is the functioning .js code that does what I want. it shows and hides the fields depending on the selected layout.

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();
    }
}

What I would like to do is take the above global array and get it into the javascript somehow, so that if new custom post types are added they will be affected by the javascript as well.

I currently have the php and javascript in 2 files, and I do not know how to get the information from the php file to the javascript.

Any help would be great

  • 写回答

2条回答 默认 最新

  • duanfan8699 2013-12-16 21:28
    关注

    You should use $_SESSION variables to share information within your pages.

    In your first page, before loading your javascript file:

    session_start();
    $_SESSION['globalVar'] = Value;
    

    In your external javascript file (with .php extension) you can get this like:

    <?php session_start(); //at the top of your JavaScript?>
    ...
    var globalVar = '<?php echo $_SESSION['globalVar']; ?>'
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题