使用 wp_localize_script 本地化 jQuery

Localize jQuery using wp_localize_script

我正在对我为 WordPress 创建的自定义主题进行本地化。我在尝试本地化 jQuery 文件时遇到问题。我懂一点 JS 和 jQuery 但还不够明白我哪里做错了。

函数如下:

function inspire_enqueue_scripts() {
    $script_path = get_template_directory_uri() . '/js/';
    $css_path = get_template_directory_uri() . '/css/';
    

    if ( is_front_page() || is_category() ) { 
        wp_enqueue_script('ci-isotope', $script_path . 'ci-isotope.js', array( 'jquery' ) );
        wp_localize_script('ci-isotope', 'ci_isotope_vars', array(
                                                             'loadmore' => __('Load more', 'inspire')
                                                            )
        );
    }
    

}
add_action( 'wp_enqueue_scripts', 'inspire_enqueue_scripts' );

这是 jQuery 文件中我尝试翻译文本的部分。

//append load more button
$container.after('<button id="load-more" class="ciLink-button"> <span class="ciLink-button-text">' ci_isotope_vars.loadmore '</span> <span class="ciLink-button-icon"> </span> </button>');

你能帮忙解决这个问题吗?谢谢。

我找到了解决方案:

$container.after('<button id="load-more" class="ciLink-button"> <span class="ciLink-button-text">Load More</span> <span class="ciLink-button-icon"> </span> </button>');
$(".ciLink-button-text").text( ci_isotope_vars.loadmore);

您似乎缺少串联(加号)。试试这个

$container.after('<button id="load-more" class="ciLink-button"> <span class="ciLink-button-text">' + ci_isotope_vars.loadmore + '</span> <span class="ciLink-button-icon"> </span> </button>');