Quantcast
Channel: How can I fetch loop of post titles via AJAX? - WordPress Development Stack Exchange
Viewing all articles
Browse latest Browse all 2

How can I fetch loop of post titles via AJAX?

$
0
0

I have a list of the most recent post titles in sidebar.php. Here is an example of how that code looks:

<?php $args = array('posts_per_page' => 20); ?>
<?php $sidebar = new WP_Query($args); ?>
<?php if ( $sidebar->have_posts() ) : ?>
<?php while ( $sidebar->have_posts() ) : $sidebar->the_post(); ?>

<div class="story">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
  <?php the_title(); ?> - <?php the_time("F j, Y h:i A"); ?>
</a>
</div>

<?php endwhile; ?>
<?php endif; ?>

That part works perfectly. It displays the 20 latest post titles and post times wrapped in permalinks. However, I am trying to do a bit more. I want to create a load more button at the bottom to fetch the next 20 post titles. I know my jQuery and that is not the issue.

I need help with figuring out how to create a custom loop in a new custom .php template file that only generates the html above. That file needs to be able to accept a parameter for a page number, so that my javascript can fetch an incremented URL each time.

I would appreciate any help, thanks!


Viewing all articles
Browse latest Browse all 2

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>