Php 通过typeahead.js在wordpress的ajax搜索结果中提供图像缩略图建议
我试图通过以下方式在WordPress搜索结果中实现typeahead.js 我想通过更改以下代码来扩展woocommerce产品搜索的功能Php 通过typeahead.js在wordpress的ajax搜索结果中提供图像缩略图建议,php,ajax,wordpress,woocommerce,typeahead.js,Php,Ajax,Wordpress,Woocommerce,Typeahead.js,我试图通过以下方式在WordPress搜索结果中实现typeahead.js 我想通过更改以下代码来扩展woocommerce产品搜索的功能 ( function($) { $( '.woocommerce-product-search input[name="s"]' ) .typeahead( { name: 'search', remote: wp_typeahead.ajaxurl + '?action=ajax_search&fn=get
( function($) {
$( '.woocommerce-product-search input[name="s"]' )
.typeahead( {
name: 'search',
remote: wp_typeahead.ajaxurl + '?action=ajax_search&fn=get_ajax_search&terms=%QUERY',
template: [
'<p><a href="{{url}}"><img src="{{img_url}}" />{{value}}</a></p>',
].join(''),
engine: Hogan
} )
.keypress( function(e) {
if ( 13 == e.which ) {
$(this).parents( 'form' ).submit();
return false;
}
}
);
} )(jQuery);
在这里,我更改了以下代码:
$image_url = wp_get_attachment_thumb_url( $the_post->ID );
$results[] = array(
'value' => $title,
'img_url' => $image_url,
'url' => get_permalink( $the_post->ID ),
'tokens' => explode( ' ', $title ),
);
<img src="false">
$img_url = wp_get_attachment_url( get_post_thumbnail_id($the_post->ID) );
当我搜索产品时,它不会显示图像,而是完美地显示标题。当我通过firebug检查元素时,它显示以下代码:
$image_url = wp_get_attachment_thumb_url( $the_post->ID );
$results[] = array(
'value' => $title,
'img_url' => $image_url,
'url' => get_permalink( $the_post->ID ),
'tokens' => explode( ' ', $title ),
);
<img src="false">
$img_url = wp_get_attachment_url( get_post_thumbnail_id($the_post->ID) );
如何解决此问题?最后,我通过添加以下代码找到了解决方案:
$image_url = wp_get_attachment_thumb_url( $the_post->ID );
$results[] = array(
'value' => $title,
'img_url' => $image_url,
'url' => get_permalink( $the_post->ID ),
'tokens' => explode( ' ', $title ),
);
<img src="false">
$img_url = wp_get_attachment_url( get_post_thumbnail_id($the_post->ID) );
当未设置缩略图时,这仍将为false。