Ajax Load More for Related posts
-
Hi, I’m using this tutorial to accomplish related posts without a plugin https://wpcrumbs.com/2017/03/how-to-display-related-posts-without-a-plugin/#comment-5948 , and I’m trying to add ajax load more to display even more podcasts.
So I’m adding this code to my functions
/** * Related posts * * @global object $post * @param array $args * @return */ function wcr_related_posts($args = array()) { global $post; // default args $args = wp_parse_args($args, array( 'post_id' => !empty($post) ? $post->ID : '', 'taxonomy' => 'category', 'limit' => 3, 'post_type' => !empty($post) ? $post->post_type : 'post', 'orderby' => 'date', 'order' => 'DESC' )); // check taxonomy $registered_taxonomies = get_taxonomies(); if (!in_array($args['taxonomy'], $registered_taxonomies)) { return; } // post taxonomies $taxonomies = wp_get_post_terms($args['post_id'], $args['taxonomy'], array('fields' => 'ids')); if (empty($taxonomies)) { return; } // query $related_posts = get_posts(array( 'post__not_in' => (array) $args['post_id'], 'post_type' => $args['post_type'], 'tax_query' => array( array( 'taxonomy' => $args['taxonomy'], 'field' => 'term_id', 'terms' => $taxonomies ), ), 'posts_per_page' => $args['limit'], 'orderby' => $args['orderby'], 'order' => $args['order'] )); include( locate_template('relatedposts.php', false, false) ); wp_reset_postdata(); }then I include this in the single.php like this
<?php wcr_related_posts(); ?>and add ajax load more like this
if(!empty($related_posts)){ $cat = get_query_var('cat'); $category = get_category ($cat); echo do_shortcode('[ajax_load_more container_type="div" css_classes="flex-box-row flex-wrap" repeater="template_2" post_type="post" posts_per_page="3" post_format="standard" category="'.$category->slug.'" cache="true" cache_id="cache-'.$category->slug.'" offset="6" pause="true" transition_container="false" images_loaded="true"]'); } ?>And this brings the posts from all categories, not related to the post categories.
So my question is, do you think there is a way I can change the $variable to import the related_posts categories.
Thanks
The topic ‘Ajax Load More for Related posts’ is closed to new replies.