帮助一个wp自定义循环,我有一个带变量的js脚本
<script>
var prueba='chihuahua';
</script>
我需要将此var传递给我的自定义循环:
<?php
$catname = "<script>document.write(prueba);</script>";
echo $catname; // show the correct pass js variable to php variable
$posts = get_posts('category_name='.$catname);
foreach ($posts as $post) {
echo "hola";
}
?>
但不显示帖子,谢谢4帮助
答案 0 :(得分:0)
你应该使用带有$ .get,$ .ajax,$ .load等的jQuery AJAX函数。这是一个例子。
JS:
<script>
$ = jQuery;
$.ajax({
type: "POST",
url: "your_code.php",
data: { prueba: "chihuahua" }
}).done(function(){
//do some code
});
<script>
PHP:
<?php
$catname = $_POST['prueba'];
echo $catname; // show the correct pass js variable to php variable
$posts = get_posts('category_name='.$catname);
foreach ($posts as $post) {
echo "hola";
}
?>
希望有所帮助。 :)
答案 1 :(得分:0)
谢谢!最终的代码是:
JS:
<script>
$(document).ready(function () {
$('.state').click(function (event){
event.preventDefault();
var id = this.id;
$('.info-container').slideDown();
$.ajax({
type: "POST",
url: "../folder/wp-content/themes/theme/content-infoContainer.php",
data: { prueba: id },
success:function(response){
if(response != null && typeof response != undefined){
$('#resultado').html(response);
}
else{
$('#resultado').html('<p>ERROR</p>');
}
}
});
});
});
</script>
PHP:
<?php include_once '../../../wp-blog-header.php';
$catname = $_POST['prueba'];
$category = get_the_category($catname);
global $post;
$posts = get_posts('category_name='.$catname);?>
<div class="container"><div class="col-xs-12 relative">
<p class="close">X
<script>
$('.close').click(function (event) {
event.preventDefault();
$('.info-container').slideUp();
});
</script>
</p>
<?php
foreach ($posts as $post) :
setup_postdata($post);?>
<div class="row">
<h2 class="distribuidor text-center">
<?php the_field('title'); ?>
</h2>
</div>
<div class="row">
<div class="col-xs-5">
<p>
Contacto: <?php the_field('contacto'); ?>
</p>
<p>
Telefono: <?php the_field('telefono'); ?>
</p>
<p>
Mail: <?php the_field('mail'); ?>
</p>
<p>
Ciudad: <?php the_field('ciudad'); ?>
</p>
</div>
<div class="col-xs-3 col-sm-offset-4">
<div class="row">
<a href="<?php the_field('map_url'); ?>" class="pin" target="_blank">
<img src="<?php PRINT IMAGES ?>/pin.png" alt="pin">
</a>
<p class="text-center">
ver mapa
</p>
</div>
<div class="row">
<h1 class="state-heading text-center">
<?php echo $catname; ?>
</h1>
</div>
</div>
</div>
<?php
endforeach;?>
</div></div>
谢谢@Brandon