这是我google时的第一个链接,所以我会在这里发布解决方案。
所以我通过使用Ajax实现了这个效果:
AJAX 是 Asynchronous JavaScript and XML 的缩写,这项技术帮助我们从服务器加载数据而无需刷新浏览器页面。
详情:https://api.jquery.com/jquery.ajax/
图表有助于理解:https://i.stack.imgur.com/Zqyrn.gif
我创建了 jQuery,它从悬停的链接获取 url 并通过 Ajax 将其发送到服务器,如果处理成功,它会呈现 data 返回:
var hrefValue;
jQuery(document).ready(function($) {
$('#bio-box').find('a').mouseover(function() {
hrefValue = ($(this).attr('href'))
//console.log(hrefValue)
$.ajax({
url: frontendajax.ajaxurl,
type: 'POST',
data: {
'action': 'image',
'php_test': hrefValue
},
success: function(data){
$('#featured-image').html(data);
//console.log(data);
}
});
});
});
这个函数生成data:
function fimg() {
if ( isset( $_POST['php_test'] ) ) {
$testing = sanitize_text_field( wp_unslash( $_POST['php_test'] ) );
$post_id = url_to_postid( $testing );
echo get_the_post_thumbnail( $post_id );
}
die();
}
add_action( 'wp_ajax_image', 'fimg' );
add_action( 'wp_ajax_nopriv_image', 'fimg' );
HTML:
<div id="featured-image”>
</div>
它正在工作,并解决了问题。我希望它会帮助某人。请注意,整个事情仍然需要一些卫生。