Wordpress Limit posts on home page - php

I am developing a wordpress blog but for some reason the posts won't limit on the home page. I am trying to limit to 3 but it falls back to what is set in the administration area instead of 2. How come?
What am I doing wrong?
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query('posts_per_page=-1&paged=' . $paged);
query_posts('showposts = 2');
$flag = 1;
while ($wp_query->have_posts()) : $wp_query->the_post();
?>

Try using this code in the functions.php to limit the posts on the homepage, it should limit the posts to 2:
function posts_on_homepage( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'posts_per_page', 2 );
}
}
add_action( 'pre_get_posts', 'posts_on_homepage' );

There are 2 option available to do your task, you can easily limit post on your home page.
1) From the Dashboard:
(a) Settings->
(b) Reading ->
(c) Blog Posts - set to 3
Or you can do this....
2) From the Dashboard:
(a) Appearance
(b) Editor
(c) home page file (usually index.php)
(d) Locate this line:
(e) Just before this line add this line:
I hope this process will work for you, if you need more help I'm always available to help you

You cannot use WP_Query and query_posts together. And you should never use query_posts. You should either use pre_get_posts or WP_Query.
You should set posts_per_page to 3 if you need to display 3 posts.

Related

WordPress Pagination problem for one category - pagination fix

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.

/page/2/ doesn't work on category page - Wordpress

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

Change number of posts to display in category page

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.

Wordpress: have template output all posts with category x where x is assigned dynamically

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

Next and previous post link in same terms custom taxonomy

I've created a custom taxonomy => cat-blog in my custom post => blog, cat-blog have 4 terms and each terms have list of post belong to that term
Example of terms:
- City Updates (4 post belong)
- Home Tips (6 post belong)
- Real Estate Guide (8 post belong)
- Real Estate Industry (9 post belong)
and using this query
<?php
$query = new WP_Query(array('posts_per_page' => 2, 'post_type' => 'blog', 'blog-cat' => get_the_term_list( $post->ID, 'blog-cat' )));
while ($query->have_posts()) : $query->the_post();
?>
<?php
// content here
?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<?php
?>
to display 2 post in same category,
AND I just want to put next and prev pagination, so I can navigate the rest of the post, belong to that term.
Don't ever change the main query for a custom query on archive pages and on the home page. The main query already does what you want to do. Trying to run a custom query to try and get the same result is like reinventing the wheel. It also causes problems with pagination
SOLUTION
First, remove your custom query, and return to the main loop. The following is all you need in your taxonomy.php
if( have_posts() ) {
while( have_posts() ) {
the_post();
//REST OF YOUR LOOP
}
}
Use pre_get_posts in conjuction with the conditional tags if you need to alter the main query. For instance, if you need 2 posts per page on your taxonomy page, do the following in functions.php
function so26499451_custom_ppp( $query ) {
if ( !is_admin() && $query->is_tax() && $query->is_main_query() ) {
$query->set( 'posts_per_page', '2' );
}
}
add_action( 'pre_get_posts', 'so26499451_custom_ppp' );
You can now paginate as normal without any problems. You will now see two posts from the specific term that you clicked on per page on your taxonomy.php.

Categories