我这里有一些代码,当您单击某些链接时,它会重新加载页面,并根据我设置的查询数组对我的帖子进行排序。我只是想知道如何让它通过ajax加载和排序,而不是重新加载页面?
以下是我使用的代码:
<?php
$sort= esc_attr($_GET[\'sort\']);
if($sort == "title_asc") { $order= "&orderby=title&order=ASC"; }
if($sort == "title_desc") { $order= "&orderby=title&order=DESC"; }
if($sort == "date_desc") { $order= "&orderby=date&order=DESC"; }
if($sort == "date_asc") { $order= "&orderby=date&order=ASC"; }
?>
<div class="elementsToFilter">
<ul>
<li><a href="?sort=title_asc" <?php if ($sort == "title_asc"){ echo \'style="color:gray"\'; } ?>>Title : A - Z</a></li>
<li><a href="?sort=title_desc" <?php if ($sort == "title_desc"){ echo \'style="color:gray"\'; } ?>>Title : Z - A</a></li>
<li><a href="?sort=date_desc" <?php if ($sort == "date_desc"){ echo \'style="color:gray"\'; } ?>>Date : Newest - Oldest</a></li>
<li><a href="?sort=date_asc" <?php if ($sort == "date_asc"){ echo \'style="color:gray"\'; } ?>>Date : Oldest - Newest</a></li>
</ul>
</div>
<?php $the_query = new WP_Query( \'cat=3309&posts_per_page=12\'. $order); ?>
<?php while($the_query->have_posts()): $the_query->the_post(); ?>
<?php if($view_type == \'grid\'): ?>
<?php get_template_part(\'partials/blog/content-grid\', \'loop\'); ?>
<?php else: ?>
<?php get_template_part(\'partials/blog/content-list\', \'loop\'); ?>
<?php endif; ?>
<?php endwhile; ?>