0

so i having trouble figuring this problem out maybe someone will have the solution for me, im loading more posts on click through ajax and i want to hide the load more button when there are no more posts to load. I know what values to compare in order to acheive this the problem is they are in my functions.php file and the button is in my template file. how can i pass these values to the template? thanks in advance!

functions.php

function get_publications() {

    if ( isset($_REQUEST) ) :

            // global $maxpost,$mypage;
    $args = array( 'post_type' => 'publications', 'posts_per_page' => 2, 'offset' => 2 );
    $loop = new WP_Query( $args );

            $mypage = (get_query_var('paged')) ? get_query_var('paged') : 1;
            $maxpost = $loop->max_num_pages;

    while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <section class="col-sm-10 col-sm-offset-1 col-md-8 col-md-offset-2  publications-section tagged-posts post">
      <?php if ( has_post_thumbnail() ) : ?>
      <a href="<?php echo get_permalink( $post->ID ); ?>">
        <div class="col-sm-4 col-sm-push-8 publications-section-image" style="background-image:url('<?php the_post_thumbnail_url(); ?>');"></div>
      </a>
      <?php endif; ?>
      <div class="col-sm-8 col-sm-pull-4">
       <h2><a href="<?php echo get_permalink( $post->ID ); ?>"><?php the_title(); ?></a></h2>
       <p><?php the_excerpt(); ?></p>
       <small><?php echo get_post_meta($post->ID, "_contact", true); ?></small>
       <small class="pub-tags loaded"> <span><?php the_terms( $post->ID,'mytag','#',' #'); ?></span></small>
     </div>
    </section>
    <?php echo ++$mypage; echo $maxpost; endwhile;
endif;
die();
}
 add_action( 'wp_ajax_get_publications', 'get_publications' );
 add_action( 'wp_ajax_nopriv_get_publications', 'get_publications' );

load.js

$(document).ready(function(){
$('#loadmore').click(function(event) {
if (event.preventDefault) {
    event.preventDefault();
} else {
    event.returnValue = false;
}

  $.ajax({
      url: ajaxurl,
      data: {
          'action' : 'get_publications',
      },
      success:function(data) {
        $('#posts section:last-child').after(data);

        // console.log(max_post_vars.posts);
      }
  });
  console.log('loadmore clicked');
  return false;
  });

  });

template file

$args = array(
          'post_type' => 'publications',
          'posts_per_page' => 2,
      );

    $query = new WP_Query( $args ); ?>

    <?php if ($query->have_posts()) : ?>
      <div id="posts">
        <?php while ($query->have_posts()) : $query->the_post(); ?>
          <section class="col-sm-10 col-sm-offset-1 col-md-8 col-md-offset-2  publications-section tagged-posts post">
            <?php if ( has_post_thumbnail() ) : ?>
            <a href="<?php echo get_permalink( $post->ID ); ?>">
              <div class="col-sm-4 col-sm-push-8 publications-section-image" style="background-image:url('<?php the_post_thumbnail_url(); ?>');"></div>
            </a>
            <?php endif; ?>
            <div class="col-sm-8 col-sm-pull-4">
             <h2><a href="<?php echo get_permalink( $post->ID ); ?>"><?php the_title(); ?></a></h2>
             <p><?php the_excerpt(); ?></p>
             <small><?php echo get_post_meta($post->ID, "_contact", true); ?></small>
             <small class="pub-tags pub"> <span><?php the_terms( $post->ID,'mytag','',' '); ?></span></small>
           </div>
          </section>
        <?php echo $mypage; endwhile; wp_reset_postdata();  ?>
      </div>

      <div class="col-sm-10 col-sm-offset-1 col-md-8 col-md-offset-2" id="pub-btn-section">
        <input type="submit" class="btn" id="loadmore" name="" value="<?php the_field('load_more',pll_current_language('slug')); ?>">
      </div>
    <?php endif;  ?>
1
  • You would need to send the number of posts in the response, but you're making it harder on yourself by sending HTML as a response because you can't get any useful information from the response. If you send a JSON response then you could include data about how many posts are remaining. Or at the very least you could count the number of posts returned on the front end. Commented Oct 25, 2017 at 3:56

1 Answer 1

1

You could get your get_publications functions to return a JSON object with all sorts of information you need.

e.g

function get_publications() {
    // Your query here.

    $data = [
        'html' => $your_html,
        'found_posts' => $query->found_posts,
        'current_post' => $query->current_post,
        'current_page' => $page,
        etc...
    ];

    echo json_encode($data);

}

And then parse that data with your javascript.

4
  • thanks it works just as expected, only thing is i can't call the wordpress functions from inside the string now. any thoughts? Commented Oct 3, 2017 at 14:00
  • Sorry, I don't understand you will have to explain a bit more. In your js or template or functions? Commented Oct 3, 2017 at 22:35
  • so in the functions.php i have wordpress functions inside the html, when i make everything a string and pass it, the values those methods return don't show up. Commented Oct 4, 2017 at 12:57
  • You would have to show me, otherwise it's too hard to tell what could be happening. But an easy way to store html from a function like that is to use ob_start and ob_get_clean. Commented Oct 4, 2017 at 23:08

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.