Created
August 24, 2018 01:13
-
-
Save gwhitcher/a7eeb9a55b0f1971d475e2680366f831 to your computer and use it in GitHub Desktop.
Easy WordPress AJAX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Ajax for filter (replace DOMAIN.com with your domain name) | |
$("#postFilterLinks a").click(function(e) { | |
e.preventDefault(); | |
var dataID = $(this).attr("data-id") | |
$.ajax({ | |
type: "GET", | |
url: "http://DOMAIN.com/posts-filter", | |
data: "categoryID="+dataID, | |
success: function(html) { | |
$("#postsFiltered").html(html); | |
} | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* The template for displaying the homepage. | |
* | |
* Template name: Homepage | |
* | |
* @package | |
*/ | |
get_header(); | |
?> | |
<div id="postFilter"> | |
<div id="postFilterLinks"> | |
<ul class="list-inline"> | |
<li class="list-inline-item"><a class="filterLink" href="" data-id="0">All</a></li> | |
<?php | |
$categories = get_categories(); | |
foreach($categories as $category) { | |
echo '<li class="list-inline-item"><a href="" data-id="'.$category->term_id.'">'.$category->name.'</a></li>'; | |
} | |
?> | |
</ul> | |
</div> | |
<h2>Latest News</h2> | |
<div id="postsFiltered"> | |
<?php | |
$args = array( | |
'post_type' => 'post', | |
'posts_per_page' => -1, | |
); | |
$loop = new WP_Query( $args ); | |
while ( $loop->have_posts() ) : $loop->the_post(); ?> | |
<a id="id-<?php the_id(); ?>" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"> | |
<h3><?php the_title(); ?></h3> | |
</a> | |
<?php endwhile; ?> | |
<?php wp_reset_query(); ?> | |
</div> | |
</div> | |
<?php get_footer(); ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* The template for displaying the Posts Filter. (make sure this pages url is posts-filter otherwise change it in the default.js | |
* | |
* Template name: Posts Filter | |
* | |
* @package | |
*/ | |
if(!empty($_GET['categoryID'])) { | |
$categoryID = $_GET['categoryID']; | |
$args = array( | |
'cat' => $categoryID, | |
'post_type' => 'post', | |
'posts_per_page' => -1, | |
); | |
} else{ | |
$args = array( | |
'post_type' => 'post', | |
'posts_per_page' => -1, | |
); | |
} | |
$loop = new WP_Query( $args ); | |
while ( $loop->have_posts() ) : $loop->the_post(); ?> | |
<a id="id-<?php the_id(); ?>" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"> | |
<h3><?php the_title(); ?></h3> | |
</a> | |
<?php endwhile; ?> | |
<?php wp_reset_query(); ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment