Wordpress: Limit posts per page in category - php

In my index.php I use this code to limit the posts per page and it works without any problems:
$showposts = 5;
$do_not_show_stickies = 1;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('category__in' => $cat, 'showposts' => $showposts, 'ignore_sticky_posts' => 1, 'paged' => $paged);
$loop2query = new WP_Query($args);
query_posts($args); if(have_posts()) : while (have_posts()) : the_post(); ?>
<div class="blogpost"> ... </div>
<?php endwhile; endif;
posts_nav_link(); // Navigating the pages with $showposts each. ?>
The same code did not work in category.php so I changed it to the following but it still does not work:
$showposts = 4;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
if (have_posts()) { while (have_posts()) { the_post(); ?>
<div class="blogpost"> ... </div>
<?php } }
else { ?>
<p>There are no blog posts in this category.</p>
<?php } ?>
<?php posts_nav_link(); // Navigating the pages with $showposts each. ?>
I tried to change the line with if(have_posts()) : while (have_posts()) : the_post(); ?> [...] in category.php to make it similar to that line in index.php but nothing I tried worked.

Wordpress has a setting for this, found in the admin area under SETTINGS -> READING -> Blog pages show at most
You can use this instead of custom-modifying your queries. It may make it a little easier to maintain your project down the road.

Use the posts_per_page argument (Codex here)
$args = array('category__in' => $cat, 'posts_per_page' => $showposts, 'ignore_sticky_posts' => 1, 'paged' => $paged);

In you first example, you actually have two queries : new WP_query, then query_posts, so you need to get rid of one of them, as this is redundant. In the second exemple, it is the contrary, you do not have any query (although WordPress might execute one by default, depending on where this page is called). So anyway, there is no point in using $showposts in your 2nd example, as you are not executing a query after...
if (have_posts()) is generally used to treat a default (not visible in your page code) loop from WordPress or to treat a query that you declare just before (generally with query_posts()).
As #Samuel is saying, the argument to use is posts_per_page, but I think you are not there yet and you should first start to learn how to execute a query, so you can start by reading the WordPress codex on query_posts, it will be the best place to go first : http://codex.wordpress.org/Function_Reference/query_posts.

Related

wp AJAX load more plugin repeating the same previous posts

I using wp ajax load more plugin While clicking the button it repeating the same previous post. How to fix it. Here I share my code in below:
<?php
$the_query = new WP_Query( array(
'posts_per_page'=>10,//on loading page i show 10 after click load more i want to show other posts
'post_type'=>'post-name',
'category_name' => 'A-E',
'orderby'=> 'title',
'order' => 'ASC',
'paged' => get_query_var('paged') ? get_query_var('paged') : 1)
);
?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
// here I print the following data
<?php
endwhile;
?>
<?php
echo do_shortcode('[ajax_load_more post_type="post-name" posts_per_page="10" category="a-e" button_label="Load More"]');
?>
can anyone fix it?
I just use this to solve my own problem:
while ( $query->have_posts() ) : $query->the_post();
$do_not_duplicate[] = $post->ID; // Store post ID in array
// Other loop actions could go here
endwhile; wp_reset_query();
$post__not_in = ($do_not_duplicate) ? implode(',', $do_not_duplicate) : '';
echo do_shortcode('[ajax_load_more post__not_in="'. $post__not_in .'" post_type="post-name" posts_per_page="10" category="a-e" button_label="Load More"]');
If you want to get the next set of pages with ajax-load-more you must use the offset parameter. The ajax-load-more plugin must get the next posts that WP_Query has. So both WP_Query and ajax-load-more must query the same pages. First alter the shortcode to be same as WP_Query, by adding order and orderby parameter:
[ajax_load_more post_type="post-name" posts_per_page="10" order="ASC" orderby="title" category="a-e" button_label="Load More"]
Then add offset=(get_query_var('paged') ? get_query_var('paged') : 1)*10 like this:
[ajax_load_more post_type="post-name" offset='.((get_query_var('paged') ? get_query_var('paged') : 1)*10).' posts_per_page="10" order="ASC" orderby="title" category="a-e" button_label="Load More"]
Note: Seems like ajax-load-more doesn't know of - so you must separate category or post_type by ,. Unless you have actually a category named a-e
You don't need to create a custom query. You are using a plugin that will generate it for you via their shortcode. As per their documentation, you can just create your settings for the shortcode.
See instructions here https://connekthq.com/plugins/ajax-load-more/docs/shortcode-builder/
Just take the generated shortcode and paste it into your page/post or in your php file with do_shortcode.
The plugin handles the query and the posts per page etc.

Custom tag.php template

I'm very new to PHP and have some trouble wrapping my head around it sometimes so please bear with me.
I have a lot of categories and a lot of tags. I started making category-slug.php templates but it'd probably be best for me to just use category.php and tag.php templates. I just can't get them to work unless I add in something like 'category_name' => 'art'. I've also read that querying isn't ideal (I think that's what I'm doing?), but I have had custom development done and I'm not sure if that has or hasn't been left as my only option.
$page_content = "";
if (have_posts()) :
while (have_posts()) : the_post();
$page_content .= get_the_content();
endwhile;
endif;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'post', 'posts_per_page' => 6, 'paged'
=> $paged );
And then later on I have this with post title, date, excerpts, etc. to follow.
<?php
$wp_query = new WP_Query($args);
while ( $wp_query->have_posts() ) : $wp_query->the_post();
?>
How do I make the category.php and tag.php pages specific to each unique slug without having to manually make each one?
Category Template Hierarchy, as described in WP codex:
The Template Hierarchy specifies that WordPress will use the first Template file it finds in your current Theme's directory from the following list:
category-slug.php
category-ID.php
category.php
archive.php
index.php
You don't have to do any custom WP_Query to get the posts of category.
if (have_posts()) :
while (have_posts()) : the_post();
the_content();//this is the content of the single post, belonging to this category
the_title();//title of the single post
the_excerpt();//excerpt of the single post
//and so on
endwhile;
endif;
It's the similar scenario for tags as well.
More details on Category Templates and Tag Templates.

Pagination : Page 3 Not Found in a WordPress installation

My problem is pagination which I am using in Wordpress. I used the plugin WP Pagenavi. I'm not really sure what is wrong with it.
I found the answer to my problem using this code :
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
<?php query_posts('cat=4&posts_per_page=15'.'&paged=' . $paged); ?>
But to my disappointment, when I reduced the posts_per_page to 5, I CAN get the pagination to work until page 2 but when I click page 3 and so on, WordPress can't find it. I used another solution from my research:
<?php
$limit = '5';
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts('cat=4&showposts=' . $limit . '&paged=' . $paged);
$wp_query->is_archive = true; $wp_query->is_home = false;
?>
It still didn't help. I don't want to touch functions.php. I'm only editing category.php.
Check my block of code below :
<?php if (is_category('category1')) { ?>
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
<?php query_posts('cat=1&posts_per_page=15'.'&paged=' . $paged); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<!-- SOME CODE TO POST THE POST -->
<?php endwhile; ?>
<?php wp_pagenavi() ?>
<?php endif; ?>
<?php } else if (is_category('category2')) { ?>
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
<?php query_posts('cat=2&posts_per_page=15'.'&paged=' . $paged); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<!-- SOME CODE TO POST THE POST -->
<?php endwhile; ?>
<?php wp_pagenavi() ?>
<?php endif; ?>
<?php } else if (is_category('category3')) { ?>
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
<?php query_posts('cat=3&posts_per_page=5'.'&paged=' . $paged); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<!-- SOME CODE TO POST THE POST -->
<?php endwhile; ?>
<?php wp_pagenavi() ?>
<?php endif; ?>
<?php } else if (is_category('category4')) { ?>
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
<?php query_posts('cat=4&posts_per_page=5'.'&paged=' . $paged); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<!-- SOME CODE TO POST THE POST -->
<?php endwhile; ?>
<?php wp_pagenavi() ?>
<?php endif; ?>
<?php } else { ?>
<!-- SOME CODE -->
<?php } ?>
Please note that category1 and category2 display 5 posts while the other 2 categories will display 15 posts. And these are all in the category.php.I don't want to use the # of posts set in the Settings > Reading.
If you think the if statement and putting also the cat ID is redundant, well, it does not get posts of that category name.
UPDATE
I used this code:
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
<?php $args = array ('post_type' => 'post','cat' => '4','posts_per_page' => '5','paged' => $paged);?>
<?php $my_query = new WP_Query ($args);?>
Again, it worked BUT it only shows two pages! I don't even know where the 3rd page is, supposedly there should be a 3rd page.
Your code has some serious issues
Never ever use query_posts, ever. It breaks the main query object on which so many plugins and functionalities rely, it also breaks pagination and fails silently, so it is really hard to debug pagination when it does fail. If you really really have to use a custom query, use WP_Query instead. You should take your time to read this post and all of the linked posts. It is really helpful as it tells you why you should not use query_posts and when should you use custom queries and when not
This point are coupled to the first one and the linked post. You must never change the main query to use a custom one on the homepage or any type of archive page. This always causes much more issues than what is actually solves. Always use pre_get_posts to alter the main query before it runs. This way, you lets the main query handle all the heavy lifting correctly without you breaking a sweat.
Now, to fix your issue:
First of all, remove all your queries, and just add this code in your category.php (Remember to replace your pagination function, wp_pagenavi())
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
// Your loop with template tags and html mark up
}
wp_pagenavi();
}
You will immediately see that your category posts are showing correctly, but the amount of posts will be the same as what you set in the back end under reading
We will now use pre_get_posts to alter the amount of posts per category. For this, add the following code to your functions.php (Requires PHP 5.3+ and the code is untested)
add_action( 'pre_get_posts', function ( $q )
{
if ( !is_admin() // Very important, otherwise back end queries will be affected as well
&& $q->is_main_query() // Very important, we just need to modify the main query
&& $q->is_category() // Only target category pages
) {
// Check on which category page we are and set posts_per_page accordingly
if ( $q->is_category( array( 1, 2 ) ) )
$q->set( 'posts_per_page', 15 );
if ( $q->is_category( array( 3, 4 ) ) )
$q->set( 'posts_per_page', 5 );
}
});
This should basically do it.
Assuming this could help for those who, like me, use a DIVI CHILD THEME !
I finally found the solution by searching divi pagination.
In fact, in my case, I built a divi child theme. However, the post per page in my custom category page is set to 3, but into my divi > theme option the Number of Posts displayed on Category page were set to 6.
That why the page 3 was displaying an error 404. So, I set it to 1.
I read on a post that the "Blog pages show at most" into the settings > reading option have to be under the post_per_page custom query (for the home page), or else, it create a 404 page.
However, it look like that the Number of Posts displayed into the Divi option, overwrite the Blog pages show at most. Here why into my category.php page, I was stuck on page 3.
I leave you my tiny code about my custom category.php page for divi theme child :
<?php
//query{
//print_r(get_queried_object());
$category = get_queried_object();
$the_cat_nicename = $category->slug;
$the_cat_name = $category->name;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args_s = new WP_Query(array(
'post_type' => 'post',
'category_name' => $the_cat_nicename,
'posts_per_page' => 3,
'paged' => $paged,
'orderby'=>'date',
'order'=>'DESC'));
//query}
if ( $args_s->have_posts())
{
echo $the_cat_name;
echo '<br/>';
while ( $args_s->have_posts())
{
$args_s->the_post();
$the_id=get_the_ID($post->ID);
echo $the_id.'<br/>';
}
if ($args_s->max_num_pages > 1)
{
echo get_next_posts_link( $GLOBALS['older_post_lang'], $args_s->max_num_pages );
echo get_previous_posts_link( $GLOBALS['newer_post_lang'] );
}
}
//wordpress _have_posts}
?>
Do not forget to set your Number of Posts displayed on Category page to 1 into your Divi > theme option.
We have finally arrived to a final answer and working code!
If you read Mr. Pieter Goosen's answer above, it will help you. It really helped me. So, I'm going to give you the final answers. This is actually custom number of post in every category disregarding what is set in the settings.
So in my category.php
<?php if (is_category('category1')) { ?>
<?php if ( have_posts() ) {
while ( have_posts() ) {
the_post();
// Your loop with template tags and html mark up
}
wp_pagenavi();
}
?>
<?php } else if (is_category('category2')) { ?>
<?php if ( have_posts() ) {
while ( have_posts() ) {
the_post();
// Your loop with template tags and html mark up
}
wp_pagenavi();
}
?>
<?php } else if (is_category('category3')) { ?>
<?php if ( have_posts() ) {
while ( have_posts() ) {
the_post();
// Your loop with template tags and html mark up
}
wp_pagenavi();
}
?>
<?php } else if (is_category('category4')) { ?>
<?php if ( have_posts() ) {
while ( have_posts() ) {
the_post();
// Your loop with template tags and html mark up
}
wp_pagenavi();
}
?>
<?php } ?>
WHERE category1 and category2 will display 5 posts, category3 and category4 will display 15 posts.
This is what you're going to place in your functions.php.
add_action( 'pre_get_posts', function($q) {
if (!is_admin() && $q->is_main_query() && $q->is_category()) {
if ($q->is_category( array(1,2) )) {
$q->set('posts_per_page', 5);
}
if ($q->is_category( array(3,4) )) {
$q->set('posts_per_page', 15);
}
}
return;
});
WHERE the numbers inside the array are category IDs.
If you compare my code and Sir Pieter's, it's almost the same BUT I added a return; before the add_action() is closed. But I'm quoting what Sir Pieter said:
It is really strange that you should return. pre_get_posts is a
action, not a filter. But any way, glad it is solved
If it still doesn't work for you, my permalink is set to:
http://www.example.com/sample-post/[/%postname%/].
I also have a WP No Category Base plugin to eliminate /category/ in my category URL. So instead of www.example.com/category/category1, it will be www.example.com/category1.

Query all posts in a certain category

I've been trying to show all post in a page of a certain category. Here's what I did so far:
<?php
$args = array(
'category_name' => 'diy-tutorial',
'post_type' => 'post',
'posts_per_page' => 3
//'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
);
query_posts($args);
$x = 0;
while (have_posts()) : the_post(); ?>
It's actually working but it wouldn't let me navigate for the next pages of the categories Like for example, i have 30 posts in that certain category, so my code will show me three posts per page right? The next>> link won't show up as well as if I change the number of posts to show per page, it won't work as well...
If you have an alternative way of showing it please let me know.
sorry I am a little rusty on my wordpress, but the way I like to get posts is by passing a querystring to get_posts
$catID=get_cat_id('diy-tutorial');
$queryString="cat=".$catID."&numberposts=1000";
global $post;
//execute the query from the string
$myposts = get_posts($queryString);
//loop thru and do stuff
foreach($myposts as $post) :
setup_postdata($post);
the_post();
endforeach;
see the codex here: http://codex.wordpress.org/Template_Tags/get_posts
I believe you have to add the buttons and possibly build up the function: Read the Codex friend: https://codex.wordpress.org/Pagination#Function_Reference
Also, I believe the commented out part of your code must be active to declare paged.
<nav class="wp-prev-next">
<ul class="clearfix">
<li class="prev-link"><?php next_posts_link(__('« Older Entries', "bonestheme")) ?></li>
<li class="next-link"><?php previous_posts_link(__('Newer Entries »', "bonestheme")) ?></li>
</ul>
</nav>

pagination is not working in wordpress main page

hello there all i have a proplem that we are using one of the new themes called fancy theme the theme comes with a proplem in pagination now we are trying to fix that problem
as you see here http://www.uniblues.com/ when you press page 1,2,3 it redirects you to the same page no change only the url changes too http://www.uniblues.com/page/3/or /4 , /5 according to the page number you press here is the code that the theme uses ..
<?php
//query_posts('paged='.$paged);
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('showposts=7');
?>
any ideas ?? .. thanks
in case if some body wants to now how i manged to did this i simply used this code and it's done ..
global $query_string;
parse_str( $query_string, $my_query_array );
$paged = ( isset( $my_query_array['paged'] ) && !empty( $my_query_array['paged'] ) ) ? $my_query_array['paged'] : 1;
query_posts('post_type=post&posts_per_page=7&paged='.$paged);
?>
and it works like charm .. thanks all
Is it a theme we can download or did you develop it ?
The code you show get the last 7 articles, so the way it reacts is normal ^^
Here is, for example, the code used in twenty twelve :
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php twentytwelve_content_nav( 'nav-below' ); ?>
<?php else : ?>
[...]
It simply use the have_post() function to get the articles, and use the template called content (content.php) to show them.
And the number of post to show is set in the administration panel > Settings > Reading.
If you are developing your own theme, you should take a look at how the base themes (like twenty twelve) work.
how about
<?php
// clear any other queries that may be in use!
wp_reset_query();
// check for $_GET paged value
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// setup post arguments
$args = array( 'posts_per_page' => 7, 'paged' => $paged, );
// run our query
query_posts($args);
// start loop
if (have_posts()) : while (have_posts()) : the_post();
// if you use the <!-- more --> in your posts.
global $more;
$more = 0;
?>
<div class="post">
etc...
</div>
<?php endwhile; ?>
<div class="navigation">
<?php next_posts_link(''); ?>
<?php previous_posts_link(''); ?>
</div>
<?php else: ?>
<div><h2>Nothing found</h2><p>No posts found for that query</p></div>
<?php endif; ?>
:)

Categories