February 4, 20197 yr Hello Guys, I am a Newbie Front End Designer and I am not really good in Ajax but I'm working on how to limit the posts to 6 and put a load more button inside the loop with a filter category. I put my code inside the category.php which is the standard template for categories. Here's the code of category filter and its loop inside category.php <ul class="categories-filters"> <?php if (is_category()) { $cat = get_query_var('cat'); $this_category = get_category($cat); $this_category = wp_list_categories('hide_empty=0&hierarchical=false&order=ASC&orderby=title&show_count=0&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID."&echo=0"); if($this_category !='<li>No categories</li>') { echo '<ul class="ul-menu">'.$this_category.'</ul>'; } } ?> </ul> <div class="content"> <?php if(have_posts() ) { ?> <div id="main-content"> <div id="inside"> <?php $i = 0; ?> <?php while (have_posts()) : the_post(); if($i % 3 == 0) { ?> <div class="row ng-row"> <?php } ?> <div class="col-md-4 col-sm-12 col-xs-12 half"> <a href="<?php the_permalink();?>"> <div class="img-holder"> <?phpif ( has_post_thumbnail() ) { the_post_thumbnail(); } else { echo '<img src="' . get_bloginfo( 'stylesheet_directory' ) . '/assets/img/thumbnail-default-rev.jpg" />'; } ?> <h2 class="project-title"><?php the_title() ?></h2> <small>see more ></small> </div> </a> </div> <?php $i++; if($i != 0 && $i % 3 == 0) { ?> </div><!--/.row--> <div class="clearfix"></div> <?php } ?> <?php endwhile; } wp_reset_query(); ?> </div> </div> </div> and my ajax code for category filter inside category.php function cat_ajax_get(catID) { jQuery("a.ajax").removeClass("current"); jQuery("a.ajax").addClass("current"); //adds class current to the category menu item being displayed so you can style it with css jQuery("#loading-animation").show(); var ajaxurl = '<?php echo admin_url( 'admin-ajax.php' ); //must echo it ?>'; jQuery.ajax({ type: 'POST', url: ajaxurl, data: {"action": "load-filter", cat: catID }, success: function(response) { jQuery("#category-post-content").html(response); jQuery("#loading-animation").hide(); return false; } }); } I tried to find a solution over the internet but unluckily no suits for my code. I tried also to create an argument like post_per_page but it breaks my category filter and post the posts randomly not but categorized. I hope someone helps me. I will appreciate any helps thank you :)
February 4, 20197 yr Your best bet here would be to do the whole lot using Javascript and avoid mixing PHP and JS together, otherwise it's going to be tricky to keep track of state of already loaded items. I think for this I would use the Wordpress API and pull out all posts under that category, then use posts per page and offset to change the items displayed https://developer.wordpress.org/rest-api/using-the-rest-api/pagination/. Your load more will need to work out how many items are currently displayed and use that as your offset every time a new request is made.
February 5, 20197 yr That's a very complicated way to do this. If you just use a WP query you can set the category and limit the number of posts. You then just add the prev/next navigation. https://css-tricks.com/snippets/wordpress/run-loop-on-posts-of-specific-category/ https://developer.wordpress.org/reference/functions/query_posts/ Wordpress has done all the hard work for you - no need to reinvent the wheel.
February 5, 20197 yr Author Just now, fisicx said: That's a very complicated way to do this. If you just use a WP query you can set the category and limit the number of posts. You then just add the prev/next navigation. https://css-tricks.com/snippets/wordpress/run-loop-on-posts-of-specific-category/ https://developer.wordpress.org/reference/functions/query_posts/ Wordpress has done all the hard work for you - no need to reinvent the wheel. Hello @fisicx once I used the WP Query my posts were showing randomly and the category filter is not working, once I clicked a category, its randomly query_posts( 'posts_per_page=5' ); Edited February 5, 20197 yr by Sicario
February 5, 20197 yr Author Just now, Jack said: Your best bet here would be to do the whole lot using Javascript and avoid mixing PHP and JS together, otherwise it's going to be tricky to keep track of state of already loaded items. I think for this I would use the Wordpress API and pull out all posts under that category, then use posts per page and offset to change the items displayed https://developer.wordpress.org/rest-api/using-the-rest-api/pagination/. Your load more will need to work out how many items are currently displayed and use that as your offset every time a new request is made. Can you show me an example of it? @Jack
February 5, 20197 yr 32 minutes ago, Sicario said: Hello @fisicx once I used the WP Query my posts were showing randomly and the category filter is not working, once I clicked a category, its randomly query_posts( 'posts_per_page=5' ); It's because you are not formatting the query correctly: $query = new WP_Query( array( 'posts_per_page' => 5, 'cat' => 99 ) ); if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); //do something; } wp_reset_postdata(); } It's all detailed in the links I provided. Once you understand how the loop works you will discover a whole load of WP functions you can use.
February 5, 20197 yr Author Just now, fisicx said: It's because you are not formatting the query correctly: $query = new WP_Query( array( 'posts_per_page' => 5, 'cat' => 99 ) ); if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); //do something; } wp_reset_postdata(); } It's all detailed in the links I provided. Once you understand how the loop works you will discover a whole load of WP functions you can use. Yeah I understand it but the query you are showing is pertaining on category with the ID 99, my category filter will not work if i follow it
February 5, 20197 yr 2 hours ago, Sicario said: Yeah I understand it but the query you are showing is pertaining on category with the ID 99, my category filter will not work if i follow it Is your filter a form? Lets say you have a dropdown: <select name="category"> <option value="98">Cat 1</option> <option value="99">Cat 2</option> </select> When the form is submitted you can grab the value from the URL, sanitise it, and pass it into WP Query. $category = filter_input( INPUT_GET, 'category', FILTER_SANITIZE_SPECIAL_CHARS ); $query = new WP_Query( array( 'posts_per_page' => 5, 'cat' => $category ) );
Create an account or sign in to comment