我已经为我的WooCommerce产品创建了自定义元字段。然而,我使用的插件(高级Woo搜索)是用JavaScript编写的,我需要访问PHP变量。我用过wp_localize_script()
这样做。然而,在前端,没有显示我的数据。
以下是我的PHP代码:
function js_enqueue_scripts() {
global $post;
$text2 = get_post_meta( $post->ID, \'_load_speed_field\', true );
$text3 = get_post_meta( $post->ID, \'_tyre_brand_field\', true );
$text4 = get_post_meta( $post->ID, \'_brand_model_field\', true );
$text5 = get_post_meta( $post->ID, \'_run_flat_field\', true );
//Put your variables inside a array
$arrayname = array(
\'text1\' => $text1,
\'text2\' => $text2
);
//Register Script
wp_register_script( "advanced-woo-search-pro", plugin_dir_url( __FILE__ ) . "/assets/js/common.js" );
//Load the Script on Front-End
wp_enqueue_script( "advanced-woo-search-pro" );
//Localize script and pass the Data
wp_localize_script( "advanced-woo-search-pro", "prefix", $arrayname );
}
//Works function on wp_enqueue_scripts Hook
add_action( "wp_enqueue_scripts", "js_enqueue_scripts" );
下面是我访问插件数据的JavaScript:html += result.title + prefix.text1 + prefix.text2;
我在插件文档中找到了这个过滤器。有人能帮助我将自定义字段连接到标题搜索结果吗?谢谢