没有真正了解你的主要问题。。您是否尝试比较两种帖子类型并将其显示在页面模板中?或者您是否尝试将当前页面与选定的帖子类型进行比较?
尝试创建页面模板
<?php
/*
* Template Name: Compare
*/
?>
<?php
get_header();
?>
<form id="customsearch" action="" method="post">
<select name="compare-one" id="compare-one">
<option value="130">1</option>
<option value="102">2</option>
</select>
<select name="compare-two" id="compare-two">
<option value="130">1</option>
<option value="102">2</option>
</select>
</form>
<script type="text/javascript">
$(document).ready(function() {
$(\'#compare-one\').change(function() {
var compareone_value = $(\'#compare-one\').val();
var data = {
\'action\':\'callback_function\',
\'compare-one\':compareone_value,
};
$.post(ajax_object.ajaxurl, data, function(response) {
$(\'#compare-one-container\').html(response);
});
});
$(\'#compare-two\').change(function() {
var comparetwo_value = $(\'#compare-two\').val();
var data = {
\'action\':\'callback_function\',
\'compare-two\':comparetwo_value,
};
$.post(ajax_object.ajaxurl, data, function(response) {
$(\'#compare-two-container\').html(response);
});
});
});
</script>
<div id="compare-container">
<div id="compare-one-container">
</div>
<div id="compare-two-container">
</div>
</div>
<?php get_footer(); ?>
接下来,向函数中添加回调函数。php
add_action(\'wp_ajax_callback_function\', \'callback_function\');
add_action(\'wp_ajax_nopriv_callback_function\', \'callback_function\'); // for not logged in user
function callback_function() {
if (isset($_POST[\'compare-one\']) || isset($_POST[\'compare-two\'])) {
$arr_id = array($_POST[\'compare-one\'],$_POST[\'compare-two\']);
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query(array(\'post_type\' => \'listing\', \'posts_per_page\' => 2, \'post__in\' => $arr_id));
while ($wp_query->have_posts()) : $wp_query->the_post();
$custom = get_post_custom($post->ID);
$beds = $custom[\'_beds\'][0];
$baths = $custom[\'_baths\'][0];
apply_filters(\'the_title\', the_title());
apply_filters(\'the_content\', the_content());
endwhile;
$wp_query = null; $wp_query = $temp;
rewind_posts();
}
die();
}
?>
别忘了本地化WordPress ajax脚本,以便在首页中使用
wp_enqueue_script( \'ajax-script\', \'ajax_object\', array(\'jquery\'));
wp_localize_script( \'ajax-script\', \'ajax_object\', array( \'ajaxurl\' => admin_url( \'admin-ajax.php\' ) ) ); // setting