代码之家  ›  专栏  ›  技术社区  ›  Rodrick

如何使用WP Store Locator在前端显示自定义元数据?

  •  0
  • Rodrick  · 技术社区  · 6 年前

    我想在商店信息下方的信息窗口(电子邮件下方)添加自定义文本。

    example

    我遵照以下指示:

    https://wpstorelocator.co/document/add-custom-meta-data-to-store-locations/

    因此,我在worpress主题的function.php文件中添加了以下内容:

    // adding my_textinput field
    add_filter( 'wpsl_meta_box_fields', 'custom_meta_box_fields' );
    
    function custom_meta_box_fields( $meta_fields ) {
    
        $meta_fields[__( 'Additional Information', 'wpsl' )] = array(
            'phone' => array(
                'label' => __( 'Tel', 'wpsl' )
            ),
            'fax' => array(
                'label' => __( 'Fax', 'wpsl' )
            ),
            'email' => array(
                'label' => __( 'Email', 'wpsl' )
            ),
            'url' => array(
                'label' => __( 'Url', 'wpsl' )
            ),
            'my_textinput' => array(
                'label' => __( 'Textinput', 'wpsl' )
            )
        );
    
        return $meta_fields;
    }
    
    // add my_textinput in to the json response
    function custom_frontend_meta_fields( $store_fields ) {
    
        $store_fields['wpsl_my_textinput'] = array( 
            'name' => 'my_textinput',
            'type' => 'text'
        );
    
        return $store_fields;
    }
    
    
    // show my_textinput into the frontend on info window
    add_filter( 'wpsl_info_window_template', 'custom_info_window_template' );
    
    function custom_info_window_template() {
    
        global $wpsl_settings, $wpsl;
    
        $info_window_template = '<div data-store-id="<%= id %>" class="wpsl-info-window">' . "\r\n";
        $info_window_template .= "\t\t" . '<p>' . "\r\n";
        $info_window_template .= "\t\t\t" .  wpsl_store_header_template() . "\r\n";  
        $info_window_template .= "\t\t\t" . '<span><%= address %></span>' . "\r\n";
        $info_window_template .= "\t\t\t" . '<% if ( address2 ) { %>' . "\r\n";
        $info_window_template .= "\t\t\t" . '<span><%= address2 %></span>' . "\r\n";
        $info_window_template .= "\t\t\t" . '<% } %>' . "\r\n";
        $info_window_template .= "\t\t\t" . '<span>' . wpsl_address_format_placeholders() . '</span>' . "\r\n";
        $info_window_template .= "\t\t" . '</p>' . "\r\n";
    
        $info_window_template .= "\t\t" . '<% if ( phone ) { %>' . "\r\n";
        $info_window_template .= "\t\t" . '<span><strong>' . esc_html( $wpsl->i18n->get_translation( 'phone_label', __( 'Phone', 'wpsl' ) ) ) . '</strong>: <%= formatPhoneNumber( phone ) %>(1)</span>' . "\r\n";
        $info_window_template .= "\t\t" . '<% } %>' . "\r\n";
        $info_window_template .= "\t\t" . '<% if ( fax ) { %>' . "\r\n";
        $info_window_template .= "\t\t" . '<span><strong>' . esc_html( $wpsl->i18n->get_translation( 'fax_label', __( 'Fax', 'wpsl' ) ) ) . '</strong>: <%= fax %>(2)</span>' . "\r\n";
        $info_window_template .= "\t\t" . '<% } %>' . "\r\n";
        $info_window_template .= "\t\t" . '<% if ( email ) { %>' . "\r\n";
        $info_window_template .= "\t\t" . '<span><strong>' . esc_html( $wpsl->i18n->get_translation( 'email_label', __( 'Email', 'wpsl' ) ) ) . '</strong>: <%= formatEmail( email ) %>(3)</span>' . "\r\n";
        $info_window_template .= "\t\t" . '<% } %>' . "\r\n";
    
        /**
         * Include the data from a custom field called 'my_textinput'.
         * 
         * Before you can access the 'my_textinput' data in the template, 
         * you first need to make sure the data is included in the JSON output.
         * 
         * You can make the data accessible through the wpsl_frontend_meta_fields filter.
         * 
         * 
         */
    
        $info_window_template .= "\t\t" . '<% if ( my_textinput ) { %>' . "\r\n";
        $info_window_template .= "\t\t" . '<p><%= my_textinput %></p>' . "\r\n";
        $info_window_template .= "\t\t" . '<% } %>' . "\r\n";
    
        $info_window_template .= "\t\t" . '<%= createInfoWindowActions( id ) %>' . "\r\n";
        $info_window_template .= "\t" . '</div>' . "\r\n";
    
        return $info_window_template;
    }
    

    当我添加以下代码(几乎是结尾)时,信息窗口不会显示:

    $info_window_template .= "\t\t" . '<% if ( my_textinput ) { %>' . "\r\n";
    $info_window_template .= "\t\t" . '<p><%= my_textinput %></p>' . "\r\n";
    $info_window_template .= "\t\t" . '<% } %>' . "\r\n";
    

    我在DevTool控制台上得到以下错误:

    Uncaught ReferenceError: my_textinput is not defined
        at eval (eval at m.template (underscore.min.js?ver=1.8.3:5), <anonymous>:52:2)
        at c (underscore.min.js?ver=1.8.3:5)
        at H (wpsl-gmap.min.js?ver=2.2.15:1)
        at _.ze.<anonymous> (wpsl-gmap.min.js?ver=2.2.15:1)
        at Object.trigger ...
    

    如何确保数据包含在JSON输出中?

    1 回复  |  直到 6 年前
        1
  •  0
  •   Dileep Kumar Awasthi    6 年前

    有3个过滤器需要使用来实现它。

    1. wpsl_meta_box_字段
    2. wpsl_frontend_meta_字段
    3. wpsl_info_window_模板

    wpsl_frontend_meta_fields筛选器,用于在frontend json中添加自定义字段。

    上述错误表示自定义字段my_textinput未添加到前端json中。请检查是否正在使用缓存。如果是,请从设置中清除缓存。对于此导航,请转到“存储定位器设置”页中的“工具”部分,然后清除“存储定位器瞬态缓存”。 Clear transient cache