我一直在关注本教程:http://pixelers.net/filter-wordpress-posts-by-category-with-ajax/。和工作。用AJAX过滤WordPress帖子
我想创建一个基于流行和最近的帖子的过滤器。对于基于大量正在查看的帖子的热门帖子。
对于“最近”提出的最后一篇文章。仅针对“热门”显示帖子,根据查看次数最多的次数显示。
的index.php
<div class="col-sm-12">
<ul class="post-filters">
<li><a href="">Recent</a></li>
<li><a href="">Popular</a></li>
</ul>
</div>
<main id="main" class="content site-main">
<?php $query_args = array(
'post_type' => 'post',
'meta_key' => 'wpb_post_views_count',
'orderby' => 'meta_value_num',
);
$the_query = new WP_Query($query_args);
?>
<?php if ($the_query->have_posts()) : ?>
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<div class="col-md-3">
<?php get_template_part('template-parts/content', 'grid'); ?>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<?php get_template_part('content', 'none'); ?>
<?php endif; ?>
</main>
ajax.php
jQuery(document).ready(function ($) {
var $mainContent = $('#main'),
$cat_links = $('.post-filters li a');
$cat_links.on('click', function (e) {
e.preventDefault();
$el = $(this);
var value = $el.attr("href");
$mainContent.animate({opacity: "0.7"});
$mainContent.load(value + " #main", function(){
$mainContent.animate({opacity: "1"});
});
});
});
我怎样才能做一个链接最近流行可以点击并过滤。
谢谢。
谢谢@Prakash饶, 另一个quoestion,这里我把 $(体).load(functi on(){ ........ }); 我把它放在头部闭合之前,它不起作用。 – Yudi
把它放在footer.php(footer.php必须加载在最近和最近发布的文件中) –
非常感谢@Prakash Rao。 问题是身体没有定义,请问我知道为什么? 对不起,我还是一个初学者,这个WordPress主题是我从另一个主题修改的主题:) – Yudi