如何将变量连接到搜索结果标题?

时间:2019-07-03 作者:NDT-AM

我正在尝试使用高级Woo搜索插件将我的帖子元数据连接到我的搜索结果标题。文档提供了以下过滤器,但我不确定输入什么作为参数:

enter image description here

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

// 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 \'<div class="product_custom_field">\';
    // 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 \'</div>\';
}

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\' );

1 个回复
最合适的回答,由SO网友:Michael Davis 整理而成

请尝试使用以下代码

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;

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

相关推荐

Testing Plugins for Multisite

我最近发布了一个WordPress插件,它在单个站点上非常有效。我被告知该插件在多站点安装上不能正常工作,我理解其中的一些原因。我已经更新了代码,现在需要一种方法来测试更新后的代码,然后才能转到实时客户的多站点安装。我有一个用于测试的WordPress安装程序的单站点安装,但需要在多站点安装上进行测试。根据我所能找到的唯一方法是在网络上至少有两个站点来安装整个多站点安装,以测试我的插件。设置WordPress的整个多站点安装是插件开发人员的唯一/首选方式,还是有更快的测试环境可用。