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

时间:2018-07-14 16:18:13

标签: javascript json wordpress google-maps

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

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 个答案:

答案 0 :(得分:0)

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

  1. wpsl_meta_box_fields
  2. wpsl_frontend_meta_fields
  3. wpsl_info_window_template

wpsl_frontend_meta_fields过滤器,用于在前端json中添加自定义字段。

提到的错误意味着自定义字段my_textinput没有添加到前端json中。请检查您是否正在使用缓存。如果是,请清除设置中的缓存。为此,请转到“商店定位器设置”页面中的“工具”部分,然后清除“商店定位器瞬态缓存”。 Clear transient cache