Currently by default Wordpress display 8 posts in category.php page, i need to increase or decrease limit.
How to get it?
Thank you.
Use the pre_get_posts filter to modify the number of posts displayed on categories.
Example:
function wpse_modify_category_posts_per_page( $query ) {
// Check we're on the frontend and modifying the main query.
if ( ! is_admin() && $query->is_main_query() ) {
// Change to 8 posts per page when viewing a category.
if ( $query->is_category() ) {
$query->set( 'posts_per_page', 8 );
}
}
}
add_action( 'pre_get_posts', 'wpse_modify_category_posts_per_page' );
Here we begin by checking we're not in the admin and we're affecting the main query. Then we test whether we're viewing a category page.
If all of those tests are passed we use the set method to change the number of posts per page.
Further reading: http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
In your category.php file, before the while loop, add this
// Get the last 10 posts in the special_cat category.
<?php query_posts( 'posts_per_page=10' ); ?>
Replace 10 with your desired number.
Related
somebody tried to fix pagination problem on my site by adding a function to your functions.php file[![screenshot - console][1]][1]
unfortunately, after updating wordpress or acf, this function does not work and when you try to go to the next page in the "atom" category, it displays 404 - Sorry, this page does not exist.
The pagination problem concerns only one category (subcategory). In the functions.php file I found a function like this:
function fix_atom_category_paged_query( $q ) {
if ( ! is_admin() && is_category( 'atom' ) && $_GET['debug'] == 1 ) {
$q->set( 'post_type', 'post' );
$q->set( 'posts_per_page', 9 );
// wp_die( var_dump( $q ) );
return $q;
}
}
add_action( 'pre_get_posts', 'fix_atom_category_paged_query', 2, 1 ); ```
[1]: https://i.stack.imgur.com/rKqzT.png
I suspect that the problem with the category called "atom" is due to the canonical link building and the wordpress core build itself. Names such as rss, feed, rss2, rdf, atom may conflict.
Ok, I tested it on another site. If category is called "atom", then wordpress pagination does not work for the archive of this category. I think this is WordPress problem.
I'm busy with this for two days now. I have the following setup in Wordpress:
Permalinks:
/%category%/%postname%/
Files (among others):
category-blog
category-podcast
category-ebooks
Created categories:
blog (standard)
podcast
ebooks
The links site.com/blog, site.com/podcast and site.com/ebooks work, but when I go to site.com/blog/page2/ the page can't be found. Same problem with the other category pages.
I can see the posts in the category, but can't access page 2 etc.
When I create a new post type it works, but not for the standard Wordpress posts. Also, I don't want to create a new post type but want to use categories.
What can I do?
You can try below code.
<?php
function paginated_category( $query ) {
if ( ! is_admin() && $query->is_main_query() ) {
if ( $query->is_category() ) {
$query->set( 'posts_per_page', 2 );
}
}
}
add_action( 'pre_get_posts', 'paginated_category' );
?>
I found this solution (after days of search) which seems to work:
Fix 404 errors
I am trying to create a series of pages that display the posts within a single category. To do this I use the following PHP code:
<?php
$args = array( 'category' => '$CATEGORY', 'numberposts' => 10000000000);
$myposts = get_posts( $args );
foreach($myposts as $post) :
setup_postdata($post);
?>
My problem is that the $CATEGORY does not seem to contain the category as a string. I have tried using both %s and $id but as I have not declared that it is the category id I am wanting it fails to work. The resulting output has been either an error or all the posts regardless of category.
What argument will convey the category string?
Below is a page illustrating the problem. This is a category page, meaning it should hold all the necessary info. If it was working it would only show the topmost post as it is the only post en site that has the "Press Release" category. Worth mentioning that I have another page just like it called "Dokument" and it displays the press release.
Page: http://www.skinwellness.se/category/pressrelease/
EDIT
I did not notice this before, but it seems that you are using the bundled theme twentythirteen. Simply delete the category.php from your child theme. If you have made changes to the parent theme directly, you should get a fresh copy of the theme, and create a child theme with all your modifications. Never make changes to a theme that you did not write. When such themes update, all you changes will be gone forever
You should then just need the pre_get_posts section in your child theme's functions.php to make everyone work
ORIGINAL ANSWER
You problem is purely your custom loop. As explained in the linked post, you should not be using custom queries in place of the main query on any type of archive page or on your home page
To solve this, revert back to the default loop. This is all you should have. No get_posts or foreach loops
if( have_posts() ) {
while( have_posts() ) {
the_post();
// Add your loop elements here like the_title() and the_content()
}
}
This should fix the problem that when you visit a category page, only the category been viewed will be viewed, no posts from other categories will be shown
Now, if you need to change anything on your category page, use pre_get_posts to do that. Make use of the is_category() conditional tag to target your category pages only. You can also target a specific category with this tag
Say for instance, you need to change the posts per page on you category page, as your case, display all posts with no pagination, you can do this in your functions.php
function so26589648_category_ppp( $query ) {
if ( !is_admin() && $query->is_category() && $query->is_main_query() ) {
$query->set( 'posts_per_page', '-1' );
}
}
add_action( 'pre_get_posts', 'so26589648_category_ppp' );
If you need to for example change the order to ASC and need to order posts by author, you can do this
function so26589648_category_ppp( $query ) {
if ( !is_admin() && $query->is_category() && $query->is_main_query() ) {
$query->set( 'posts_per_page', '-1' );
$query->set( 'order', 'ASC' );
$query->set( 'orderby', 'author' );
}
}
add_action( 'pre_get_posts', 'so26589648_category_ppp' );
You should see WP_Query for all available parameters to use
I have a template where there is a main latest featured post (tagged as featured), and then 8 more below it.
On the homepage, i was able to query for the latest featured post, and then pass its id to a function in the pre_get_posts wordpress filter. it works great there
function mh_exclude_featured_query( $query ) {
if(is_home()){
if ( $query->is_home() ) {
$feat = get_featured_post();
$query->query_vars['post__not_in'] = array($feat->ID);
}
}
}
add_action( 'pre_get_posts', 'mh_exclude_featured_query' );
but i'm also trying to do the same thing in the category.php, where i would show the latest post tagged as featured from that category. and then the remaining posts below with the featured post excluded.
Unfortnately, when i try the same method as above by using the pre_get_posts filter, i get stuck in an infinite loop and run out of memory.
if($query->is_category() && $query->is_main_query()){
$cur_cat_id = get_cat_id( single_cat_title("",false) );
$feat = get_featured_post($cur_cat_id);
$query->query_vars['post__not_in'] = array($feat->ID);
}
not sure what i'm doing differently that leads to a memory exhaustion. the category.php and index.php are near identical in their structure.
use the pre_get_posts filter:
<?php
function excludePostId($query) {
$postIds = array(
24, 10
);
if (is_category() && is_main_query()) {
set_query_var('post__not_in', $postIds);
}
}
add_action('pre_get_posts', 'excludePostId');
is_category() will accept a category slug or ID. You can limit which categories the post will be excluded from.
Add This Code
<?php if(!is_category('category_id')){
echo 'Your Code Here';
}?>
I have set my wordpress blog's frontpage to show only three posts.
On the archive.php template, when the posts of a tag are viewed, I want to show 10 results.
How do I do this?
I tried this php code. But instead of only showing the posts with a certain tag, it queries all recent posts.
//in archive.php (before the loop)
query_posts('posts_per_page=10');
Just append the query with a tag parameter (as shown here: http://codex.wordpress.org/Function_Reference/query_posts#Tag_Parameters):
query_posts('posts_per_page=10&tag=your_desired_tag');
EDIT: If you use this within a function you can also just append your limit to the original query like this:
function my_archive_loop($content) {
global $query_string;
query_posts($query_string . "&posts_per_page=10");
}
The variable $query_string should than include all the default parameters like the current tag, category, year or whatever archive page you are viewing.
can you try this:
function post_per_page_control( $query ) {
if ( is_admin() || ! $query->is_main_query() )
return;
// For archive.You can omit this
if ( is_archive() ) {
//control the numbers of post displayed/listed (eg here 10)
$query->set( 'posts_per_page', 10 );
return;
}
// For your tag
if ( is_tag() ) {
//control the numbers of post displayed/listed (eg here 10)
$query->set( 'posts_per_page', 10 );
return;
}
}
add_action( 'pre_get_posts', 'post_per_page_control' );
read more here:
1 http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
2 http://codex.wordpress.org/Function_Reference/query_posts