首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何连接变量以搜索结果标题?

如何连接变量以搜索结果标题?
EN

WordPress Development用户
提问于 2019-07-03 13:40:56
回答 1查看 340关注 0票数 0

我试图连接我的文章元数据到我的搜索结果标题使用高级Woo搜索插件。文档给出了以下过滤器,但我不确定输入什么作为参数:

下面是我到目前为止访问post元数据并将其存储在变量中的代码。我所需要的帮助是将它与上面的过滤器挂钩,以使我的搜索结果标题不同。

代码语言:javascript
运行
复制
// Display Fields
add_action('woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields');
// Save Fields
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');

function woocommerce_product_custom_fields() {
    global $woocommerce, $post;
    echo '';
    // Custom Product Text Field 1
    woocommerce_wp_text_input(
        array(
            'id' => '_tyre_size_field',
            'placeholder' => 'Tyre Size',
            'label' => __('Tyre Size', 'woocommerce'),
            'desc_tip' => 'true'
        )
    );
    // Custom Product Text Field 2
    woocommerce_wp_text_input(
        array(
            'id' => '_load_speed_field',
            'placeholder' => 'Load Index & Speed Rating',
            'label' => __('Load Index & Speed Rating', 'woocommerce'),
            'desc_tip' => 'true'
        )
    );
    // Custom Product Text Field 3
    woocommerce_wp_text_input(
        array(
            'id' => '_tyre_brand_field',
            'placeholder' => 'Tyre Brand',
            'label' => __('Tyre Brand', 'woocommerce'),
            'desc_tip' => 'true'
        )
    );
    // Custom Product Text Field 4
    woocommerce_wp_text_input(
        array(
            'id' => '_brand_model_field',
            'placeholder' => 'Brand Model',
            'label' => __('Brand Model', 'woocommerce'),
            'desc_tip' => 'true'
        )
    );
    // Custom Product Text Field 5
    woocommerce_wp_select( array(
        'id'          => '_run_flat_field',
        'label'       => __( 'Run Flat or Non-Run Flat', 'woocommerce' ),
        'description' => __( 'Choose whether the tyre is run-flat or non-run flat.', 'woocommerce' ),
        'desc_tip'    => true,
        'options'     => array(
            ' '        => __( ' ', 'woocommerce' ),
            'RUN FLAT'    => __('RUN FLAT', 'woocommerce' ),
            'NON-RUN FLAT' => __('NON-RUN FLAT', 'woocommerce' ),
        )
    ) );
    echo '';
}

function woocommerce_product_custom_fields_save($post_id) {
    // Custom Product Text Field 1
    $woocommerce_custom_product_tyre_size_field = $_POST['_tyre_size_field'];
    if (!empty($woocommerce_custom_product_tyre_size_field))
        update_post_meta($post_id, '_tyre_size_field', esc_attr($woocommerce_custom_product_tyre_size_field));
    // Custom Product Text Field 2
    $woocommerce_custom_product_tyre_brand_field = $_POST['_tyre_brand_field'];
    if (!empty($woocommerce_custom_product_tyre_brand_field))
        update_post_meta($post_id, '_tyre_brand_field', esc_attr($woocommerce_custom_product_tyre_brand_field));
    // Custom Product Text Field 3
    $woocommerce_custom_product_brand_model_field = $_POST['_brand_model_field'];
    if (!empty($woocommerce_custom_product_brand_model_field))
        update_post_meta($post_id, '_brand_model_field', esc_attr($woocommerce_custom_product_brand_model_field));
    // Custom Product Text Field 4
    $woocommerce_custom_product_run_flat_field = $_POST['_run_flat_field'];
    if (!empty($woocommerce_custom_product_run_flat_field))
        update_post_meta($post_id, '_run_flat_field', esc_attr($woocommerce_custom_product_run_flat_field));
    // Custom Product Text Field 5
    $woocommerce_custom_product_load_speed_field = $_POST['_load_speed_field'];
    if (!empty($woocommerce_custom_product_load_speed_field))
        update_post_meta($post_id, '_load_speed_field', esc_attr($woocommerce_custom_product_load_speed_field));
}

function concatenate_fields_to_title( $title ) {
    global $post;
    $text1 = get_post_meta( $post->ID, '_tyre_size_field', true );
    $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 );
    if (stripos( $title, 'TYRE' ) == true ) {
    return $text1 . " ". $text2 . " " . $text3 . " " . $text4 . " " . $text5 . " " . $title;
    }
    return $title;
}

add_filter( 'the_title', 'concatenate_fields_to_title' );
EN

回答 1

WordPress Development用户

回答已采纳

发布于 2019-07-04 09:04:03

请尝试使用以下代码

代码语言:javascript
运行
复制
add_filter( 'aws_title_search_result', 'my_aws_title_search_result', 10, 3 );

function my_aws_title_search_result( $title, $post_id, $product ) {

    $text1 = get_post_meta( $post_id, '_tyre_size_field', true );
    $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 );

    if (stripos( $title, 'TYRE' ) == true ) {
        $title = $text1 . " ". $text2 . " " . $text3 . " " . $text4 . " " . $text5 . " " . $title;
    }

    return $title;

}

此外,在此之后,您可能需要转到插件设置页面,并单击‘清除缓存’按钮。

票数 1
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/342084

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档