如何将条件数组传递给wp_LOCALIZE_SCRIPT

时间:2019-11-21 作者:markb

我正在尝试生成自定义array 从PHP函数传递wp_localize_script 但我似乎无法设置页面条件。

我有以下功能:

function mb_scripts_settings() {

    // blanks
    $mb_ajax_form_type = $mb_get_page_slug = $mb_redirect = $mb_redirect_time = $mb_form_disable = $mb_array = \'\';


    // get the form type
    $mb_ajax_form_type      = ( is_front_page() ? \'change\'  : \'submit\'  );

    // get the page
    $mb_get_page_slug       = get_page_link();

    // generate the url for redirection
    $mb_form_area           = ( ( is_page(\'admin\') && isset($_GET[\'mbtab\']) )   ? $_GET[\'mbtab\']    : null  );
    $mb_form_area_url       = ( empty($mb_form_area)    ? \'/\' : \'/admin/?mbtab=\' . $mb_form_area . \'&mbform=1\'          );

    // if the page is admin
    if( is_page(\'admin\') ) {
        $mb_redirect        = true;
        $mb_redirect_time   = 3000;
        $mb_form_disable    = true;
    }

    // if the page is password set
    if( is_page(\'pw\') ) {
        $mb_redirect        = true;
        $mb_redirect_time   = 3000;
        $mb_form_disable    = true;
    }

    // if the page is front
    if( is_page(\'pw\') ) {
        $mb_redirect        = false;
        $mb_redirect_time   = 0;
        $mb_form_disable    = false;
        $mb_form_area = $mb_form_area_url = \'\';
    }

    // build the array
    $mb_array = array( $mb_ajax_form_type, $mb_get_page_slug, $mb_redirect, $mb_redirect_time, $mb_form_disable );

    return $mb_array;
}
然而,我收到一个错误,说我不能打电话is_front_pageis_page 循环外部。

Notice: is_front_page was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false. Please see Debugging in WordPress for more information. (This message was added in version 3.1.0.)

我试图通过获取页面id和其中的slug来实现这一点,但它没有返回任何结果-在mb_get_page_slug

function mb_scripts() {
    wp_enqueue_script( \'mbtheme_js\', mbWebOS_js . \'theme-scripts. \'.js\' );
    wp_localize_script( \'mbtheme_js\', \'mbtheme_js\', mb_scripts_settings() );
}

add_action( \'wp_enqueue_scripts\',   \'mb_scripts\'        );

1 个回复
最合适的回答,由SO网友:markb 整理而成

以以下功能结束:

// set up the settings for the theme.js
function mb_scripts_settings() {

    // blanks
    $mb_ajax_form_type = $mb_get_page_slug = $mb_redirect = $mb_redirect_time = $mb_form_disable = $mb_array = \'\';

    // get the form type
    $mb_ajax_form_type      = ( is_front_page() ? \'change\'  : \'submit\'  );

    // get the page slug from ID
    $mb_get_page_slug       = get_post_field( \'post_name\', get_the_ID() );

    // if the page is admin or password
    if( is_page( array(\'admin\', \'pw\') ) ) {
        $mb_redirect        = true;
        $mb_redirect_time   = 3000;
        $mb_form_disable    = true;

        if( is_page(\'admin\') ) {
            // generate the url for redirection
            $mb_form_area           = ( ( is_page(\'admin\') && isset($_GET[\'mbtab\']) )   ? $_GET[\'mbtab\']    : null      );
            $mb_form_area_url       = ( empty($mb_form_area)    ? \'/\' : \'/admin/?mbtab=\' . $mb_form_area . \'&mbform=1\'  );
            $mb_form_area_url       = get_home_url( $mb_form_area_url );
        }
    }

    // if the page is front
    if( is_front_page() ) {
        $mb_redirect        = false;
        $mb_redirect_time   = 0;
        $mb_form_disable    = false;
        $mb_get_page_slug   = \'front_page\';
        $mb_form_area = $mb_form_area_url = null;
    }

    // build the array
    $mb_array = array(
                $mb_ajax_form_type,
                $mb_get_page_slug,
                $mb_redirect,
                $mb_redirect_time,
                $mb_form_disable
            );

    return $mb_array;
}
当脚本排队时:

    // enqueue the theme js
    wp_enqueue_script( \'mb_mbtheme_js\', mbWebOS_js . \'scripts.\' . $mbTheme . \'.js\' );

    // localise the theme js
    // only for selected pages
    if( is_page(\'admin\') || is_front_page() ) {
        wp_localize_script( \'mb_mbtheme_js\', \'mb_mbtheme_js\', mb_scripts_settings() );
    }

相关推荐

条件多维阵列和ARRAY_MAP

我正在使用Roots Sage 9,它在WordPress中使用Laravel,并将其与高级自定义字段相结合。为此,我创建了一个控制器,在这里我使用array\\u映射遍历返回的数组,以提取所有日期。根据页面的不同,我有时返回一个包含第二级数组的数组,而在其他情况下,我只返回一个包含单个级别的数组。我遇到的问题是,在我只有一个单层数组的情况下,第二个array\\u map返回错误(array\\u map():参数#2应该是数组),因为不存在数组。我以为三元数可以解决这个问题,但事实似乎并非如此我的问题