The Terms extension provides additional functionality for infinite scrolling taxonomy terms with Ajax Load More.
The extension works by using a WP_Term_Query to retrieve taxonomy terms and returns them to Ajax Load More for display.
Repeater Template
Term information must be accessed in a Repeater Template by referencing the $term object.
1 2 3 4 5 6 7 8 9 |
<?php print_r($term); ?> <li> <h3> <a href="<?php echo get_term_link($term->term_id); ?>"> <?php echo $term->name; ?> </a> </h3> <p><?php $term->description; ?></p> </li> |
Shortcode Parameters
Use the following shortcode parameters to initiate a term query with Ajax Load More.
term_query | Enable Terms Query. (true/false) Default = ‘false’ |
---|---|
term_query_taxonomy | The taxonomy slugs to query. |
term_query_number | The number of terms to return per page. Default = 5 |
term_query_hide_empty | Whether to hide terms not assigned to any posts. Default = true |
Note: You can modify or add additional query parameters by using the alm_term_query_args filter.
Example Shortcode[ajax_load_more term_query="true" term_query_taxonomy="category, portfolio-type" term_query_number="8"]
Example
The example below will load terms from the category taxonomy and get all posts associated to each term. Click the Load Terms button to get started.
The following shortcode was used in the example above.
1 |
[ajax_load_more container_type="div" css_classes="stylefree" term_query="true" term_query_taxonomy="category" term_query_number="4" term_query_hide_empty="false" theme_repeater="terms.php" button_label="Load Terms" pause="true"] |
The following Repeater Template was used in the example above.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<div class="alm-term-example"> <h3> <a href="<?php echo get_term_link($term->term_id); ?>"><?php echo $term->name; ?> <em><?php echo $term->count; ?></em></a> </h3> <?php if($term->count > 0) : $posts = get_posts( array( 'post_type' => 'post', 'numberposts' => -1, 'fields' => 'ids', 'tax_query' => array( array( 'taxonomy' => 'category', 'field' => 'term_id', 'terms' => $term->term_id, ) ) ) ); if($posts){ echo '<ul class="stylefree">'; foreach($posts as $id){ echo '<li>'; echo '<a href="'. get_permalink($id) .'">'; echo get_the_title($id); echo '</a>'; echo '</li>'; } echo '</ul>'; } endif; ?> </div> |
Filter Hooks
The following Ajax Load More filters are available to support the modification of term query arguments.
alm_term_query_args_{id}
The alm_term_query_args_{id} filter can be used to modify or extend WP_Term_Query arguments by Ajax Load More instance ID.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
function my_cat_listing($args){ // ALM Shortcode // [ajax_load_more id="category_listing" term_query="true" term_taxonomy="category"] // 'term_listing' is the value of the 'id' parameter in the shortcode. $args['hide_empty'] = false; $args['order'] = "ASC"; return $args; } add_filter( 'alm_term_query_args_category_listing', 'my_cat_listing', 10, 2); |
This filter requires the following 2 step process:
- Set Unique ID – Create a unique ID for your ALM instance by setting a value for the id parameter in the shortcode.
[ajax_load_more id="category_listing" term_query="true" term_taxonomy="category"] - Add Filter – Create the custom filter and function for this instance by appending the unique ID to the filter name.
add_filter(‘alm_term_query_args_category_listing’, ‘my_function_name’);
When is this filter useful?
This filter is useful for users who need to extend the available term query shortcode parameters.
Check out the WP_Term_Query docs for details on the available query arguments.