我有这个自定义搜索功能,可以在Wordpress中搜索自定义元键。功能运作良好。
但是,我使用高级自定义字段添加到帖子中的所有图片现在都将其网址更改为附件ID。
根据ACF中的设置页面不应该发生这种情况:
同一字段在其他页面上正常工作,而不在搜索结果页面上。检查它如何更改搜索结果中的图像源:
此处的图片网址如何以及为何更改为附件ID?请查看下面的代码:
function custom_search_function($pieces) {
// filter to select search query
if (is_search() && !is_admin()) {
global $wpdb;
$custom_fields = array('regio','provincie');
$keywords = explode(' ', get_query_var('s'));
$query = "";
foreach ($custom_fields as $field) {
foreach ($keywords as $word) {
$query .= "((mypm1.meta_key = '".$field."')";
$query .= " AND (mypm1.meta_value LIKE '%{$word}%')) OR ";
}
}
if (!empty($query)) {
// add to where clause
$pieces['where'] = str_replace("((({$wpdb->posts}.post_title LIKE '%", "( {$query} (({$wpdb->posts}.post_title LIKE '%", $pieces['where']);
$pieces['join'] = $pieces['join'] . " INNER JOIN {$wpdb->postmeta} AS mypm1 ON ({$wpdb->posts}.ID = mypm1.post_id)";
//$pieces['groupby'] = "{$wpdb->posts}.ID";
}
}
return ($pieces);
}
add_filter('posts_clauses', 'custom_search_function', 20, 1);
编辑:这是显示我的帖子结果的代码,“foto”字段是负责显示图像的字段:
<?php foreach( $posts as $post ):
//fusion-column-last, or none for normal class
$lastclass = '';
if(++$counter % 2 === 0) {
$lastclass = ' fusion-column-last';
}
setup_postdata( $post )
?>
<div class="fusion-one-half fusion-layout-column fusion-spacing-yes<?php echo $lastclass?>" style="margin-top:0px;margin-bottom:20px;background-color:white;">
<div class="fusion-column-wrapper">
<div class="bw-search-picture">
<?php $postid = get_the_ID(); ?>
<?php //echo $postid; ?>
<img src="<?php the_field('foto', $postid); ?>" alt="<?php the_title(); ?>"/>
</div>
<div class="bw-search-content">
<h2>
<a class="green" href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
</h2>
<p class="bw-regio">Regio <?php the_field('regio'); ?></p>
<p>
<a style="color:#9C9E9F;" href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">LEES VERDER ></a>
</p>
</div>
</div>
</div>
<?php endforeach; ?>
答案 0 :(得分:3)
尝试使用缩略图:
$imgsrc = wp_get_attachment_image_src(get_post_thumbnail_id( $post_id ));
echo $imgsrc[0];
编辑,将此代码与附件ID一起使用:
$imgsrc = wp_get_attachment_image_src(get_field('foto', $postid));
echo $imgsrc[0];
答案 1 :(得分:1)
而不是使用
<img src="<?php the_field('foto', $postid); ?>" />
尝试使用
<?php $foto_url = get_field('foto', $postid); ?>
<img src="<?php echo $foto_url; ?>" />