Skip to content

Instantly share code, notes, and snippets.

@michaelmano
Last active November 21, 2016 07:51
Show Gist options
  • Save michaelmano/4ba3b42b692a7dfa9edf41abd7be0a7f to your computer and use it in GitHub Desktop.
Save michaelmano/4ba3b42b692a7dfa9edf41abd7be0a7f to your computer and use it in GitHub Desktop.
Wordpress Ajax post
$("li.terms").click(function() {
document.getElementById('content-top').scrollIntoView("smooth");
var nav = document.getElementById('ajax-nav').style.padding = "20px 0 0 40px"
$("li.terms").removeClass('current');
$(this).addClass('current');
$("#ajax-columns").addClass('loading');
var postType = $(this).attr("data-post-type");
var tax = $(this).attr("data-taxonomy-name");
var catID = $(this).attr("data-category");
var ajaxurl = '<?php echo admin_url( 'admin-ajax.php' ); ?>';
cat_ajax_get(ajaxurl, postType, tax, catID);
});
function cat_ajax_get(ajaxurl, postType, tax, catID) {
jQuery.ajax({
type: 'POST',
url: ajaxurl,
data: {"action": "load-filter", tax: tax, postType: postType, cat: catID },
success: function(response) {
$("#ajax-columns").html(response);
$("#ajax-columns").removeClass('loading');
return false;
}
});
}
add_action( 'wp_ajax_nopriv_load-filter', 'mpm_load_cat_posts' );
add_action( 'wp_ajax_load-filter', 'mpm_load_cat_posts' );
function mpm_load_cat_posts () {
$args = array(
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'DESC',
'post_type' => $_POST[ 'postType' ],
'tax_query' =>
array(
array(
'taxonomy' => $_POST[ 'tax' ],
'field' => 'id',
'terms' => $_POST[ 'cat' ],
),
),
);
$postType = $_POST[ 'postType' ];
$taxName = $_POST[ 'tax' ];
$partners = get_posts($args);
ob_start ();
foreach ( $partners as $partner ) {
if ( wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'fullsize') ) {
$feedThumb = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'fullsize')[0];
} else {
$feedThumb = ''.get_template_directory_uri().'/images/blank-feed.jpg';
}
setup_postdata( $post );
include(locate_template('includes/partial-services-masonry.php')); // this is just a loop of data thats returned. e.g. post title and so on.
}
wp_reset_postdata();
$response = ob_get_contents();
ob_end_clean();
echo $response;
die(1);
}
<?php
/*
* Template Name: Services page
* Description: A Custom Post Page for Services
*/
get_header(); ?>
<div class="contentContainer mainContent">
<a name="mainContent"></a>
<div class="wrapper">
<div class="content" id="content-top">
<?php while(have_posts()) : the_post(); ?>
<div class="entryContent">
<?php the_content(); ?>
</div><!--END EntryContent-->
<?php endwhile; ?>
<div id="ajax-columns" class="ajax-columns masonry">
<?php $args = array(
'orderby' => 'name',
'order' => 'ASC',
);
$terms = get_terms('partner-category', $args);
$args = array(
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'DESC',
'post_type' => 'partner',
);
$partners = get_posts($args);
if(count($partners) > 0) { ?>
<?php foreach ($partners as $partner) {
include(locate_template('includes/partial-services-masonry.php'));
} ?>
<?php } ?>
</div><!-- END ajax-columns masonry -->
</div><!--END Content-->
<?php $args = array(
'orderby' => 'name',
'order' => 'ASC',
'parent'=>0,
);
$terms = get_terms('partner-category', $args);
if(count($terms) > 0) { ?>
<div class="ajax-nav-container sidebar" id="ajax-nav">
<div class="section ajax">
<h5>Services Filter</h5>
<ul class="ajaxNav">
<?php
foreach ($terms as $term) { ?>
<li id="<?php echo $term->slug; ?> ajax" class="terms <?php if($term->slug == 'all' ) { echo 'current'; } ?>" data-ajax-url="<?php echo admin_url( 'admin-ajax.php' ); ?>" data-post-type="partner" data-taxonomy-name="partner-category" data-category="<?php echo $term->term_id; ?>"><?php echo $term->name; ?></li>
<?php } ?>
</ul><!--END ajaxNav -->
</div><!-- END section -->
</div><!--END ajax-nav-container-->
<?php } ?>
</div><!--END Wrapper-->
</div><!--END ContentContainer-->
<?php get_footer(); ?>
.ajaxNavContainer {
margin: 0 0 60px 0;
margin: 0 0 3.75rem 0;
}
.mainContent .sidebar.ajax-nav-container {
padding: 20px 0 0 40px;
padding: 1.25rem 0 0 2.5rem;
overflow: hidden;
}
.sidebar .section.ajax h5 {
margin-bottom: 20px;
margin-bottom: 1.25rem;
}
.ajaxNavContainer.fixed {
}
.sidebar ul.ajaxNav {
list-style: none;
width: 100%;
padding: 0;
float: right;
}
.sidebar ul.ajaxNav li {
cursor: pointer;
font-size: 14px;
font-size: 0.875rem;
font-weight: 600;
display: block;
text-align: left;
text-transform: uppercase;
position: relative;
color: #a89367;
padding: 8px 0;
padding: 0.5rem 0;
border-bottom: 1px solid rgba(168,147,103, 0.3);
padding-left: 20px;
}
.sidebar ul.ajaxNav li.current,
.sidebar ul.ajaxNav li:hover {
color: #fff;
font-weight: 600;
border-bottom: 1px solid rgba(168,147,103, 1);
}
.sidebar ul.ajaxNav li strong {
color: #fff;
}
.content .inner-content ul {
text-align: left;
overflow: hidden;
font-size: 14px;
font-size: 0.875rem;
}
.inner-content {
background: #f0f0f0;
text-align: center;
padding: 40px;
padding: 2.5rem;
border: 1px solid transparent;
}
.inner-content strong {
color: #a89367;
}
.inner-content span.link {
font-size: 12px;
font-size: 0.75rem;
text-transform: uppercase;
color: #1e1e1e;
}
.inner-content p {
color: #1e1e1e;
font-size: 14px;
font-size: 0.875rem;
line-height: 1.6;
}
.inner-content h4 {
font-weight: 600;
padding-bottom: 8px;
padding-bottom: 0.5rem;
margin: 10px 0 0 0;
margin: 0.625rem 0 0 0;
font-family: inherit;
text-transform: uppercase;
color: #1e1e1e;
line-height: 1.2;
}
.inner-content h5 {
font-weight: 300;
}
.inner-content:hover {
background: transparent;
border: 1px solid #a89367;
}
.inner-content:hover p {
color: #fff;
}
.inner-content:hover h4 {
color: #a89367;
}
.inner-content:hover strong {
color: #fff;
}
.inner-content:hover span.link {
color: #fff;
}
.ajax-columns img {
width: 100%;
}
.ajax-columns.row {
margin-left: -30px;
margin-left: -1.875rem;
margin-right: -30px;
margin-right: -1.875rem;
position: relative !important;
}
.ajax-columns.row [class*=' col-'] {
padding-left: 30px;
padding-left: 1.875rem;
padding-right: 30px;
padding-right: 1.875rem;
margin-bottom: 60px;
margin-bottom: 3.75rem;
transition: 0.3s ease-in-out all;
}
.ajax-columns.row.loading [class*=' col-'] {
display: none;
}
.ajax-columns.row.loading {
min-height: 300px;
min-height: 18.75rem;
margin-bottom: 60px;
margin-bottom: 3.75rem;
}
.ajax-columns.row.loading:after {
position: absolute;
content: '';
top: 0;
left: 0;
right: 0;
bottom: 0;
background: url('images/icons/load.gif') no-repeat 50% 50%;
background-size: 35px 35px;
background-size: 2.1875rem 2.1875rem;
z-index: 9999;
}
@media( max-width: 740px ) {
.ajax-columns.row.loading:after {
position: absolute;
content: '';
top: 0;
left: 0;
right: 0;
bottom: 0;
background: url('images/icons/load.gif') no-repeat 50% 50%;
background-size: 35px 35px;
background-size: 2.1875rem 2.1875rem;
z-index: 9999;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment