I’m new to PHP. I’m building a custom WordPress theme for a music website that sells 3-album bundles and am trying to integrate this:
-User can see the previous week’s bundle by going to the ‘Last Week’s Bundle” page. From there, there is also a sorting feature that will allow them to either sort bundles by most recent, popularity, alphabetically, or just shuffle them.
On the front-end, my colleague is integrating some AJAX that will allow the content to be served dynamically without the page reloading, as well as an infinite scrolling feature.
.
Being new to PHP, I am trying to learn which way I should go about serving content to the browser as the AJAX requests it. Looking in the WordPress Codex, I cannot find enough about returning only a certain number of posts and how to make the loop grab the next set of posts when the user hits the ‘more’ button, as well as having it sort all at the same time.
Here’s my code below:
<?php
$sort = $_REQUEST['name'];
if($sort = "recent") {
$query = new WP_Query( array ( 'orderby' => 'date', 'order' => 'ASC', 'post_count' => 3 ) );
if (have_posts()) : while($query->have_posts()) : $query->the_post();
}
if($sort = "alphabetize") {
$query2 = new WP_Query( array ( 'orderby' => 'name', 'order' => 'ASC', 'post_count' => 3 ) );
if (have_posts()) : while($query2->have_posts()) : $query2->the_post();
}
if($sort = "shuffle") {
$query3 = new WP_Query( array ( 'orderby' => 'rand', 'post_count' => 3 ) );
if (have_posts()) : while($query3->have_posts()) : $query3->the_post();
}
?>
Also, here’s a snapshot of the front-end:
The easy solution is to use the Infinite Scroll plugin:
http://wordpress.org/plugins/infinite-scroll/
With it you can use the code you have above and just add in support for the WordPress pagination. The plugin will then use the pagination functions to execute the query your have setup.