i need a button that load more related posts but i dont know if this is java or just php.

I'm getting related content by using this code in single.php

<!-- Related posts -->

$custom_taxterms = wp_get_object_terms($post->ID, 'networks', array('fields' => 'ids'));
$args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => 6,
    'orderby' => 'date',
    'tax_query' => array(
            'taxonomy' => 'networks',
            'field' => 'id',
            'terms' => $custom_taxterms
    'post__not_in' => array ($post->ID),
$related_items = new WP_Query($args);
if ($related_items->have_posts()) :
echo '<h3 class="related">Related Videos</h3>';
while ($related_items->have_posts()) : $related_items->the_post();
<div class="td-block-row">
    <div class="td-block-span4">
        <div class="td_module_3 td_module_wrap_related td-animation-stack td-meta-info-hide">
            <div class="td-module-image">
                <div class="td-module-thumb">
                    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                    <?php if (has_post_thumbnail()) {
                        the_post_thumbnail('td_324x235', array('alt' => get_the_title()));
                    } else {
                        echo '<img src="'.get_bloginfo('template_directory').'/images/no-thumb/td_324x235.png" alt="'.get_the_title().'" />';
                $terms = get_the_terms($post->ID, 'networks');
                if (! empty($terms)) {
                    foreach ($terms as $term) {
                        $url = get_term_link($term->slug, 'networks');
                        print "<a href='$url' class='td-post-networks-related'>{$term->name}</a>";
                <span class="duration"><i class="fa fa-clock-o" aria-hidden="true"></i> <?php $text = get_post_meta( get_the_ID(), '_video_info_duration', true ); echo ( $text ); ?></span>
            <div class="item-details">
                <h3 class="related-title">
                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                <span class="date"><i class="fa fa-calendar" aria-hidden="true"></i> <?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'; ?></span>
                <span class="views"><i class="fa fa-eye" aria-hidden="true"></i> <?php echo getPostViews(get_the_ID()); ?></span>

<!-- End related posts -->

Any idea of what i need to do this?

  • 'field' => 'id' should be 'field' => 'term_id' in your tax_query
    – Shane
    Commented Jun 22, 2017 at 17:09
  • If you change 'posts_per_page' to -1, that will get you all of the posts that match. Then instead of a while loop, you can do an incrementing loop like for($i=0; $i<6; $i++) { and reference everything with $i, and at the bottom have a button that displays more. I do think this would work best with JavaScript (not Java) rather than PHP because with PHP all that content will be loaded with the page, thus slowing things down. With JavaScript you can have the button basically lazy load the extra content.
    – WebElaine
    Commented Jun 22, 2017 at 17:16
  • To load more results, you can use either php or javascript, up to you. With php you'll probably need to paginate the results. With javascript, you could load more results with ajax, or you could hide some of the results with css and then show them with javascript.
    – Shane
    Commented Jun 22, 2017 at 17:17


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.