将帖子 ID 添加到您打开模式的链接中(这将用于将帖子 ID 传递给 ajax 函数):
<li class="slide col25">
<a href="#modal1" class="easy-modal-open" data-post-id="<?= get_the_ID(); ?>">
//img and h4
</a>
</li>
使用 wp_localize_script 将 ajaxurl 传递给您的 jquery:
wp_localize_script( 'my_modal', 'my_modal_localize', array(
ajaxurl => admin_url('admin-ajax.php')
) );
在你的functions.php中添加一个函数来查询正确的公司(就像你上面所说的那样):
add_action( 'wp_ajax_custom_function', 'get_company_modal' );
add_action( 'wp_ajax_nopriv_custom_function', 'get_company_modal' );
function get_company_modal() {
$args = array(
p => $_POST['post_id'], //this is the post id passed via jquery.post()
);
$query = new WP_Query( $args );
$result = array();
if( $query->have_posts() ) {
while( $query->have_posts() ) {
$query->the_post();
// get all your custom fields here and add them to the $result array
$result[ $custom_field_key ] = $custom_field_value;
}
}
echo json_encode( $result );
die();
}
然后使用.post()在你的jquery中添加对click事件的调用
$('.easy-modal-open').click(function(e) {
var data = {
action: 'custom_function', //see the add_action part above
post_id: $(this).data('post-id'), //this is the post id from the a data attribute
}
$.post( my_modal_localize.ajaxurl, data, function(response) {
var customFields = $.parseJSON(response);
// add the custom fields to your output via e.g. append()
}
// and THEN open the modal
var target = $(this).attr('href');
$(target).trigger('openModal');
e.preventDefault();
});
也许您需要添加$.when().then(),但这取决于具体情况。
因此,这只是一个示例,也是实现您想要的一种方式。您当然会根据自己的情况对其进行编辑。