我正在尝试使用高级Woo搜索插件将我的帖子元数据连接到我的搜索结果标题。文档提供了以下过滤器,但我不确定输入什么作为参数:
这是我迄今为止访问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\' );
最合适的回答,由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;
}
此外,在此之后,您可能需要转到插件设置页面并单击“清除缓存”按钮。