I am using the following code to currently display a specified amount (24) of my custom post types on WP, as well as using the project categories to filter through the posts dynamically.
<!-- Display Filters for Posts --->
<ul id="filters">
<li><a href="#" data-filter="*" class="selected">Everything</a></li>
<?php
$terms = get_terms("project_categories"); // get all categories, but you can use any taxonomy
$count = count($terms); //How many are they?
if ( $count > 0 ){ //If there are more than 0 terms
foreach ( $terms as $term ) { //for each term:
echo "<li><a href='#' data-filter='.".$term->slug."'>" . $term->name . "</a></li>\n";
//create a list item with the current term slug for sorting, and name for label
}
}
?>
</ul>
<!-- Filter Array --->
<div class="row">
<div class="col-md-12">
<?php $args = array( 'post_type' => 'bw_projects', 'posts_per_page' => 24 ); ?>
<?php $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<ul class="img-list" id="isotope-list">
<?php while ( $the_query->have_posts() ) : $the_query->the_post();
$termsArray = get_the_terms( $post->ID, "project_categories" ); //Get the terms for this particular item
$termsString = ""; //initialize the string that will contain the terms
foreach ( $termsArray as $term ) { // for each term
$termsString .= $term->slug.' '; //create a string that has all the slugs
}
?>
<li class="<?php echo $termsString; ?>item project home-project"> <?php // 'item' is used as an identifier (see Setp 5, line 6) ?>
<a href="<?php echo get_permalink( $post->ID ); ?>">
<?php the_post_thumbnail(); ?>
<span class="text-content"><span><img src="<?php the_field('client_logo'); ?>" alt="" class=""></span></span>
</a>
</li> <!-- end item -->
<?php endwhile; ?>
</ul> <!-- end isotope-list -->
<?php endif; ?>
</div>
</div><!-- Close Filter Array --->
This was built using the Isotope filtering, from these intructions with a few modifications. https://www.aliciaramirez.com/2014/03/integrating-isotope-with-wordpress/
UPDATE: After Marissa Comments, this is now how my code looks after the filters.
<?php $args = array( 'post_type' => 'bw_projects', 'posts_per_page' => 18 ); ?>
<?php $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<ul class="img-list" id="isotope-list">
<?php $total_posts = wp_count_posts('bw_projects');
$total_posts = $total_posts->publish;
$number_shown = 0; ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post();
$termsArray = get_the_terms( $post->ID, "project_categories" ); //Get the terms for this particular item
$termsString = ""; //initialize the string that will contain the terms
foreach ( $termsArray as $term ) { // for each term
$termsString .= $term->slug.' '; //create a string that has all the slugs
}
?>
<li class="<?php echo $termsString; ?>item project home-project"> <?php // 'item' is used as an identifier (see Setp 5, line 6) ?>
<a href="<?php echo get_permalink( $post->ID ); ?>">
<?php the_post_thumbnail(); ?>
<span class="text-content"><span><img src="<?php the_field('client_logo'); ?>" alt="" class=""></span></span>
</a>
</li> <!-- end item -->
<?php endwhile; ?>
</ul> <!-- end isotope-list -->
<?php endif; ?>
<?php if ( $number_shown < $total_posts ) { ?>
<div class="loadMore" data-offset="<?php echo $number_shown; ?>" data-total="<?php echo $total_posts; ?>">
Load More
</div>
<?php } ?>
My loadmore file is in this current state:
<?php
global $post;
$the_offset = trim( sanitize_text_field( wp_unslash( $_POST['pOffset'] ) ) );
$the_total = trim( sanitize_text_field( wp_unslash( $_POST['totalPosts'] ) ) );
$args = array( 'post_type' => 'bw_projects', 'posts_per_page' => 12, 'offset' => $the_offset );
$posts_shown = $the_offset; //Increment this every time you display a project
?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post();
$termsArray = get_the_terms( $post->ID, "project_categories" ); //Get the terms for this particular item
$termsString = ""; //initialize the string that will contain the terms
foreach ( $termsArray as $term ) { // for each term
$termsString .= $term->slug.' '; //create a string that has all the slugs
}
?>
<li class="<?php echo $termsString; ?>item project home-project"> <?php // 'item' is used as an identifier (see Setp 5, line 6) ?>
<a href="<?php echo get_permalink( $post->ID ); ?>">
<?php the_post_thumbnail(); ?>
<span class="text-content"><span><img src="<?php the_field('client_logo'); ?>" alt="" class=""></span></span>
</a>
</li> <!-- end item -->
<?php endwhile; ?>
</ul> <!-- end isotope-list -->
<?php endif; ?>
<?php } ?>
<?php //Then check if we've shown all the posts or not, and we're done.
if ( $posts_shown >= $the_total ) { ?>
<div id="all-posts-shown"></div>
<?php } ?>
On the outputted HTML the load more button is bringing in "data-total" and "data-offset"
<div class="loadMore" data-offset="0" data-total="25">
Load More
</div>
When load more is clicked the offset changes to:
<div class="loadMore" data-offset="12" data-total="25">
Load More
</div>
Except i am seeing no extra posts loading? I think i must have missed something.