通常,此脚本显示与帖子相关的标题和内容,但我想使用自定义content.php
用于post格式。
有什么方法可以使用get_template_part(\'content\'); 在这个脚本中?
<script type="text/javascript">
jQuery(document).ready(function () {
jQuery(\'.br\').click(function () {
jQuery(\'.contents\').remove();
var checked = jQuery(\'#test\').serialize();
jQuery.ajax( {
url: "<?php echo admin_url(\'admin-ajax.php\'); ?>",
data: "action=call_post&" + checked,
success: function (obj) {
var render_data = "<div class=\'contents\'>";
// This is to watch your json object
console.log(obj);
for (var i = 0; i < obj.length; i++) {
console.log(obj[i].post_title);
render_data += " <h4>" + obj[i].post_title + "</h4>";
render_data += "<p>" + obj[i].post_content + "</p>";
}
render_data += "</div>";
jQuery(render_data).appendTo(\'.mobile_brand\');
}
});
})
});
</script>
功能。php
add_action(\'wp_ajax_call_post\', \'call_post\');
add_action(\'wp_ajax_nopriv_call_post\', \'call_post\');
function call_post()
{
$params = wp_parse_args ( $_REQUEST, array(
\'mobile\' => \'some-default-brand\',
\'template\' => \'\',
));
$brand = $params[\'mobile\'];
$args = array(
\'post_type\' => \'post\',
\'meta_query\' => array(
array(
\'key\' => \'brand\',
\'value\' => $test,
) ,
) ,
);
$query = new WP_Query($args);
if( ! empty ($params[\'template\'])) {
$template = $params[\'template\'];
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
get_template_part($template);
}
}
die();
} else {
wp_send_json($query->posts);
}
}
内容。php
<div <?php post_class( \'col-lg-2 col-md-2 col-sm-3 col-xs-6 \' ); ?> id="post-<?php the_ID(); ?>">
<div class="single-post">
<div class="post-thumb" >
<a href="<?php echo esc_url( post_permalink() ); ?>">
<?php the_post_thumbnail ( \'large\', array(
\'class\' => \'img-responsive\'
) ); ?>
</a>
</div>
<div class="post-info">
<div class="post-title">
<li><a href="<?php echo esc_url( post_permalink() ); ?>">
<?php the_title(); ?></a></li>
</div>
<div class="rs"><p><?php echo get_post_meta( get_the_ID(), \'price1\', true ); ?>
<?php _e( \'\', \'mobilewebsite\' ); ?></p></div>
</div>
</div>
</div>
<?php $item_number++;
if( $item_number % 2 == 0 ) echo \'<div class="clearfix visible-xs-block"></div>\';
if( $item_number % 4 == 0 ) echo \'<div class="clearfix visible-sm-block"></div>\';
if( $item_number % 6 == 0 ) echo \'<div class="clearfix visible-md-block"></div>\';
if( $item_number % 6 == 0 ) echo \'<div class="clearfix visible-md-block"></div>\';
?>
最合适的回答,由SO网友:jgraup 整理而成
您需要编辑与AJAX操作关联的PHPcall_post
. 如果要返回不同的数据,应在稍后处理的请求中传递一个参数;
data:"action=call_post&template=custom&"+checked,
理想情况下,您可以只返回带有标志的从php呈现的数据
data:"action=call_post&template=custom&format=html&"+checked,
从PHP的角度来看,您可以在
$_GET
,
$_POST
或
$_REQUEST
.
脚本
<script type="text/javascript">
jQuery(document).ready(function () {
jQuery(\'.br\').click(function () {
jQuery(\'.contents\').remove();
var checked = jQuery(\'#test\').serialize();
jQuery.ajax( {
url: "<?php echo admin_url(\'admin-ajax.php\'); ?>",
data: "action=call_post&template=content&" + checked,
success: function (result) {
jQuery(result).appendTo(\'.mobile_brand\');
}
});
})
});
</script>
功能。php
add_action(\'wp_ajax_call_post\', \'call_post\');
add_action(\'wp_ajax_nopriv_call_post\', \'call_post\');
function call_post()
{
$params = wp_parse_args ( $_REQUEST, array(
\'mobile\' = \'some-default-brand\',
\'template\' = \'\',
));
$brand = $params[\'mobile\'];
$args = array(
\'post_type\' => \'post\',
\'meta_query\' => array(
array(
\'key\' => \'brand\',
\'value\' => $brand,
) ,
) ,
);
$query = new WP_Query($args);
if( ! empty ($params[\'template\'])) {
$template = $params[\'template\'];
// loop through your query here and render the HTML using $template
// ...
// then kill the request
die();
} else {
// or send back the posts as json
wp_send_json($query->posts);
}
}