我知道Relevansi的高级版本支持内置ACF字段,但我有免费版本。我正在研究一种方法,从数据库中获取信息,然后在搜索结果页面上显示出来。这就是我目前的想法:
/**************功能。PHP****************/
// Relevanssi does not show ACF fields - this is a work-around for specific fields in the DB
function excerpt_function($content, $post, $query) {
global $wpdb;
// Grab everything in the ACF content fields from the DB and spit it out in the search result excerpt. If new ACF modules/fields are added in the future then they need to have their \'modules_#_name_name_content\' and so on added to show up in the Relevanssi search.
$fields = $wpdb->get_col("SELECT DISTINCT(meta_value) FROM $wpdb->postmeta WHERE meta_key LIKE \'%modules_%\'");
foreach($fields as $key => $field){
$field_value = get_post_meta($post->ID, $field, TRUE);
$content .= \' \' . ( is_array($field_value) ? implode(\' \', $field_value) : $field_value );
}
return $content;
}
add_filter(\'relevanssi_excerpt_content\', \'excerpt_function\', 10, 3);
我想我需要以下方面的帮助:
$fields = $wpdb->get_col("SELECT DISTINCT(meta_value) FROM $wpdb->postmeta WHERE meta_key LIKE \'%modules_%\'");
数据库中ACF内容的名称始终以“modules\\uu0”开头。如果我能找出如何从前缀为“modules\\uu0”的元键中获取元值,那么我想我可以在搜索页面上显示结果。
以下是我的搜索结果的重要部分:
/**************搜索。PHP****************/
<h2><?php
global $wp_query;
$some_results = $wp_query->found_posts;
printf( __( \'There are \' . $some_results . \' Search Results for: %s\', \'eqh\' ), \'<strong>\' . get_search_query() . \'</strong>\' ); ?>
</h2>
<hr />
<?php if ( have_posts() ) :
while ( have_posts() ) : the_post(); ?>
<div class="large-3 medium-6 small-12 columns m-bottom">
<article class="striped clearfix" style="border:1px solid #ccc;">
<h3><a href="<?php echo get_permalink(); ?>"><?php relevanssi_the_title(); // the_title(); ?></a></h3>
<?php if( has_post_thumbnail() ){ ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $loop->post->ID ), \'single-post-thumbnail\' ); // https://stackoverflow.com/questions/32837968/how-to-get-featured-image-of-a-product-in-woocommerce ?>
<div class="m-bottom f-left m-right"><a href="<?php echo the_permalink(); ?>"><img src="<?php echo $image[0]; ?>" data-id="<?php echo $loop->post->ID; ?>"></a></div>
<?php } else { ?>
<div class="m-bottom f-left m-right"><img src="<?php echo site_url(); ?>/images/placeholder.png" width="" height="" alt="placeholder png" /></div>
<?php } ?>
<br clear="all" />
<?php echo the_excerpt(); // get_the_excerpt(); ?>
<p><a href="<?php echo the_permalink(); ?>"><?php _e( \'Read More\', \'eqh\'); ?></a></p>
</article>
</div>
<?php endwhile;
endif; ?>
最合适的回答,由SO网友:S. Jensen 整理而成
我只发布函数。PHP代码:
// Relevanssi does not show ACF fields by default - this is a work-around for specific fields in the DB
function excerpt_function($content, $post, $query) {
global $wpdb;
// Grab everything in the ACF content fields from the DB and spit it out in the search result excerpt. If new ACF modules/fields are added in the future then they need to have their \'modules_#_name_name_content\' and so on added to show up in the Relevanssi search.
$fields = $wpdb->get_col("SELECT DISTINCT(meta_key) FROM $wpdb->postmeta WHERE meta_key LIKE \'%_content\' AND meta_key NOT LIKE \'_modules%\'");
foreach($fields as $key => $field){
$field_value = get_post_meta($post->ID, $field, TRUE);
$content .= \' \' . ( is_array($field_value) ? implode(\' \', $field_value) : $field_value );
}
return $content;
}
add_filter(\'relevanssi_excerpt_content\', \'excerpt_function\', 10, 3);