我一直在研究如何使用AJAX进行循环。我在网上找到了一些代码并修改了一些部分,但我似乎无法使其正常工作。
这是我的循环代码:
<article id="main" class="five seventh padded">
<?php
$categories = get_categories(); ?>
<ul id="category-menu">
<?php foreach ( $categories as $cat ) { ?>
<li id="cat-<?php echo $cat->term_id; ?>"><a class="<?php echo $cat->slug; ?> ajax" onclick="cat_ajax_get(\'<?php echo $cat->term_id; ?>\');" href="#"><?php echo $cat->name; ?></a></li>
<?php } ?>
</ul>
<div id="loading-animation" style="display: none;"><img src="<?php echo admin_url ( \'images/loading-publish.gif\' ); ?>"/></div>
<div id="category-post-content">
<?php add_action( \'wp_ajax_nopriv_load-filter\', \'prefix_load_cat_posts\' );
add_action( \'wp_ajax_load-filter\', \'prefix_load_cat_posts\' );
function prefix_load_cat_posts () {
$cat_id = $_POST[ \'cat\' ];
$args = array (
\'cat\' => $cat_id,
\'posts_per_page\' => 10,
\'order\' => \'DESC\'
);
$posts = get_posts( $args );
ob_start ();
foreach ( $posts as $post ) {
setup_post_data( $post ); ?>
<article class="post">
<!-- Display the Title as a link to the Post\'s permalink. -->
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<!-- Display the date (November 16th, 2009 format) and a link to other posts by this posts author. -->
<div class="row pad-bottom">
<!-- Display a comma separated list of the Post\'s Categories. -->
<small class="postmetadata pull-left"><?php the_category(\', \'); ?></small>
<small class="date pull-left"> Posted by <?php the_author_posts_link() ?> on <?php the_time(\'F jS, Y\') ?></small>
</div>
<!-- Display the Post\'s content in a div box. -->
<article id="entry">
<?php the_content(\'Read More here...\'); ?>
</article>
</article> <!-- closes the first div box -->
<!-- Stop The Loop (but note the "else:" - see next line). -->
<?php } wp_reset_postdata();
$response = ob_get_contents();
ob_end_clean();
echo $response;
die(1);
}
?>
</div>
<script>
function cat_ajax_get(catID) {
jQuery("a.ajax").removeClass("current");
jQuery("a.ajax").addClass("current"); //adds class current to the category menu item being displayed so you can style it with css
jQuery("#loading-animation-2").show();
var ajaxurl = \'http://localhost/united/wp-admin/admin-ajax.php\';
jQuery.ajax({
type: \'POST\',
url: ajaxurl,
data: {"action": "load-filter", cat: catID },
success: function(data) {
console.log(data);
}
});
}
</script>
</article>
由于某种原因,当我点击一个类别时,我没有得到任何回复。任何帮助都将不胜感激。
更新:现在可以了。对于未来的参与者来说,这是:在函数中。php
<?php add_action( \'wp_ajax_nopriv_load-filter\', \'prefix_load_cat_posts\' );
add_action( \'wp_ajax_load-filter\', \'prefix_load_cat_posts\' );
function prefix_load_cat_posts () {
$cat_id = $_POST[ \'cat\' ];
$args = array (
\'cat\' => $cat_id,
\'posts_per_page\' => 10,
\'order\' => \'DESC\'
);
$posts = get_posts( $args );
global $post;
ob_start ();
foreach ( $posts as $post ) {
setup_postdata( $post ); ?>
<article class="post">
<!-- Display the Title as a link to the Post\'s permalink. -->
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<!-- Display the date (November 16th, 2009 format) and a link to other posts by this posts author. -->
<div class="row pad-bottom">
<!-- Display a comma separated list of the Post\'s Categories. -->
<small class="postmetadata pull-left"><?php the_category(\', \'); ?></small>
<small class="date pull-left"> Posted by <?php the_author_posts_link() ?> on <?php the_time(\'F jS, Y\') ?></small>
</div>
<!-- Display the Post\'s content in a div box. -->
<article id="entry">
<?php the_content(\'Read More here...\'); ?>
</article>
</article> <!-- closes the first div box -->
<!-- Stop The Loop (but note the "else:" - see next line). -->
<?php } wp_reset_postdata();
$response = ob_get_contents();
ob_end_clean();
echo $response;
die(1);
}
?>
在主题文件中:
<article id="main" class="five seventh padded">
<?php
$categories = get_categories(); ?>
<ul id="category-menu">
<?php foreach ( $categories as $cat ) { ?>
<li id="cat-<?php echo $cat->term_id; ?>"><a class="<?php echo $cat->slug; ?> ajax" onclick="cat_ajax_get(\'<?php echo $cat->term_id; ?>\');" href="#"><?php echo $cat->name; ?></a></li>
<?php } ?>
</ul>
<div id="loading-animation" style="display: none;"><img src="<?php echo admin_url ( \'images/loading-publish.gif\' ); ?>"/></div>
<div id="category-post-content">
</div>
<script>
function cat_ajax_get(catID) {
jQuery("a.ajax").removeClass("current");
jQuery("a.ajax").addClass("current"); //adds class current to the category menu item being displayed so you can style it with css
jQuery("#loading-animation-2").show();
var ajaxurl = \'http://localhost/united/wp-admin/admin-ajax.php\';
jQuery.ajax({
type: \'POST\',
url: ajaxurl,
data: {"action": "load-filter", cat: catID },
success: function(response) {
jQuery("#category-post-content").html(response);
jQuery("#loading-animation").hide();
return false;
}
});
}
</script>
</article>
SO网友:Rodrigo Zuluaga
我知道这是旧的,这看起来正是我需要的,但不能用ATM机。
我将所有内容粘贴到函数上。php是有原因的。
第一:主题文件的分类循环:用一个快捷码打印出来,因为我用的是bakery i want,不想把它放在网站的特定位置。这很好用。
<?php
function ajaxcatloop( $atts, $content = null ) {
$categories = get_categories();
$output .= \'<article id="main" class="five seventh padded">\';
$output .= \' <ul id="category-menu">\';
foreach ( $categories as $cat ) {
$output .= \'<li id="cat-\'.$cat->term_id.\'"><a class="\'.$cat->slug.\' ajax" onclick="cat_ajax_get("\'.$cat->term_id.\'");" href="#">\'.$cat->name.\'</a></li>\';
}
$output .= \'</ul>\';
$output .= \'<div id="category-post-content">\';
$output .= \'</div>\';
$output .= \'</article>\';
return $output;
}
add_shortcode( \'loopajax\', \'ajaxcatloop\' );
?>
第二:主题文件中的jquery代码:在页脚中为其编制索引以避免jquery问题。IDK,如果这是工作,但它得到了它的位置
<?php add_action(\'wp_footer\', function(){ ?>
<script>
function cat_ajax_get(catID) {
jQuery("a.ajax").removeClass("current");
jQuery("a.ajax").addClass("current"); //adds class current to the category menu item being displayed so you can style it with css
jQuery("#loading-animation-2").show();
var ajaxurl = \'<?php echo admin_url(\'admin-ajax.php\'); ?>\';
jQuery.ajax({
type: \'POST\',
url: ajaxurl,
data: {"action": "load-filter", cat: catID },
success: function(response) {
jQuery("#category-post-content").html(response);
jQuery("#loading-animation").hide();
return false;
}
});
}
</script>
<?php
});
?>
第三:将AJAX代码放在它应该放的地方。
add_action( \'wp_ajax_nopriv_load-filter\', \'prefix_load_cat_posts\' );
add_action( \'wp_ajax_load-filter\', \'prefix_load_cat_posts\' );
function prefix_load_cat_posts () {
$cat_id = $_POST[ \'cat\' ];
$args = array (
\'cat\' => $cat_id,
\'posts_per_page\' => 10,
\'order\' => \'DESC\'
);
$posts = get_posts( $args );
global $post;
ob_start ();
foreach ( $posts as $post ) {
setup_postdata( $post ); ?>
<article class="post">
<!-- Display the Title as a link to the Post\'s permalink. -->
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<!-- Display the date (November 16th, 2009 format) and a link to other posts by this posts author. -->
<div class="row pad-bottom">
<!-- Display a comma separated list of the Post\'s Categories. -->
<small class="postmetadata pull-left"><?php the_category(\', \'); ?></small>
<small class="date pull-left"> Posted by <?php the_author_posts_link() ?> on <?php the_time(\'F jS, Y\') ?></small>
</div>
<!-- Display the Post\'s content in a div box. -->
<article id="entry">
<?php the_content(\'Read More here...\'); ?>
</article>
</article> <!-- closes the first div box -->
<!-- Stop The Loop (but note the "else:" - see next line). -->
<?php } wp_reset_postdata();
$response = ob_get_contents();
ob_end_clean();
echo $response;
die(1);
}
?>
我复制了相同的代码,只是以不同的方式放置
但是,当我单击其中一个类别时,它会在这一行中出错,并且什么也没有发生。
<article id="main" class="five seventh padded"> <ul id="category-menu"><li id="cat-8"><a class="carnes ajax" onclick="cat_ajax_get("8");" href="#">Carnes</a></li><li id="cat-9"><a class="comida-saludable ajax" onclick="cat_ajax_get("9");" href="#">Comida Saludable</a></li><li id="cat-3"><a class="jugos ajax" onclick="cat_ajax_get("3");" href="#">Jugos</a></li></ul><div id="loading-animation" style="display: none;"><img src=""/></div><div id="category-post-content"></div></article>
Uncaught Syntax Error: Unexpected end of imput