pagination is not working in wordpress main page - php

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; ?>
:)

Related

Fix for wordpress loop

I am new too wordpress and have one problem. I finaly understand post loop and it work well but have one problem. For example: I have two pages of my posts (all are 6). In one page i have five posts (excerpt only) and on secound page i have one post only and in this case (when on one of my pages i have only one post it change to content post but it need to stay excerpt). So my question is what can i change in my code to make it work well in this case when on some page left only one post?
It's my loop:
<?php if (have_posts()) : ?>
<?php if (($wp_query->post_count) > 1) : ?>
<?php while (have_posts()) : the_post(); ?>
<!-- Do your post header stuff here for excerpts-->
<?php the_excerpt() ?>
<!-- Do your post footer stuff here for excerpts-->
<?php endwhile; ?>
<?php else : ?>
<?php while (have_posts()) : the_post(); ?>
<!-- Do your post header stuff here for single post-->
<?php the_content() ?>
<!-- Do your post footer stuff here for single post-->
<?php endwhile; ?>
<?php endif; ?>
<?php else : ?>
<!-- Stuff to do if there are no posts-->
<?php endif; ?>
Use the posts_per_page parameter to an array and start the loop.
$args = array( 'post_type' => 'post', 'posts_per_page' => 4,'category' => 2, 'paged' => $paged );
$wp_query = new WP_Query($args);
while ( have_posts() ) : the_post();
$content = get_the_content();
echo $content;
endwhile;
Here I have printed the 4 contents from the archive. So the page will only have 4 posts in it. You can change the value based on your need.
I have used and got the output. Hope this helps you too.
Ur second if condition should be inside while and while shuld be called once ..
Somthing like
If
While
Then if for count
Elseif
Else
End while
End if
I hope it wil help ..
Answer for all: $wp_query->post_count need to be replaced by $wp_query->found_posts and everything working fine now!

Wordpress one-page template with dynamic portfolio section

I've been searching all morning for answers to no avail. I'm building a one page WordPress template. I have a home page which uses a one page template called one-page.php that is brining in all the other pages. Heres the php from that template:
<?php
$args = array(
'post_type' => 'page',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php endwhile; endif; ?>
This code works great. All the sections can use the content-page.php template part, excluding the portfolio section which I would like to use another template part that brings all the portfolio custom post types.
I've tried to add conditional if statements to both the one-page.php and the content-page.php, like this:
<?php if ( is_page( 'portfolio' ) ) : ?>
//My portfolio custom post type loop is here
<? endif; ?>
But that didn't work either - I think that is because the is_page() function will be checking the current page being displayed which is the Home page. Rather than figuring out what page the query is currently dealing with - but I'm not sure.
Can anyone help me understand how I would go about conditionally loading the portfolio section into a separate template part?
You can achieve this checking page slug, which you can get in the loop. If it is "portfolio" (or whatever you saved), load content-portfolio.php, otherwise content-page.php. Something like this:
if (have_posts()) : while ($the_query->have_posts()) : $the_query->the_post();
if ("portfolio" === $post->post_name) {
get_template_part('content', 'portfolio');
} else {
get_template_part('content', 'page');
}
endwhile; endif;
Set condition in file
<?php
if (have_posts()) : while ($the_query->have_posts()) : $the_query->the_post();
if (is_page('portfolio')) {
get_template_part('content', 'portfolio');
} else {
get_template_part('content', 'page');
}
endwhile; endif;
?>
create a template file named content-portfolio.php which is copy of content-page.php and put the below code in it. if it show 'call' it means your template is working.
portfolio.php
<?php
echo "portfolio.php";
die('Call');
?>

WordPress Category Pagination not working

I have two custom post types from the archive.php One is archive-slug.php and the other is category-slug.php. The pagination works on the archive-slug.php but the same code on the category-slug.php won't even show. I'm somewhat new to Wordpress and php so i'm sure i'm missing something here, I just don't know what?
<?php
// Custom Post Type
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'available_dogs',
'category_name' => 'adopted-dogs',
'posts_per_page'=> 9,
'paged'=> $paged
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="dog-info-box col-lg-4 col-sm-6 col-xs-12">...</div>
<?php endwhile; ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</div>
<?php
// Get the pagination
fusion_pagination( $pages = '', $range = 2 );
?>
<?php if( $sidebar_exists == true ): ?>
<?php wp_reset_query(); ?>
As I want to inform you that when you create a file "category-slug.php : it's an default template.No need to add the query posts.
It will display the posts of the category whose slug is added in file like category-slug.php.Here is the modifying code.
if (have_posts() ) :while ( have_posts() ) : the_post();
the_content();//content is going on.
endwhile;//pagination functionfusion_pagination( $pages = '', $range = 2 );else : _e( 'Sorry, no posts matched your criteria.' ); endif;
Precaution :1:Pagination code should be placed just before of the while loop.2:In your code pagination code have been place after the endif that's why it is not working.
Thanking you.
I used this code and got it working. It's not pretty but it will do for now. For what ever reason when I try to use it after the loop it doesn't work. When I use it after the endif it works.
<?php if ($the_query->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Older Entries', $the_query->max_num_pages ); // display older posts link ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( 'Newer Entries' ); // display newer posts link ?>
</div>
</nav>
<?php } ?>
Please follow this steps.
1: Please make sure, your category have the posts.
2: If your category slug is "test" then your file name should be " category-test.php . if any confusion then please let me know.
3: See the code and write in file.
if(have_posts()) :while(have_posts()): the_post();the_title();the_content();endwhile;//pagination of wp
previous_posts_link( '« Newer Entries' );
next_posts_link( 'Older Entries »', 0 );endif;
Note:1:Please place this code in file and run if it will running successfully, then we will add the designing part.
2: If you can share the website URL then I can also give suitable solution because then I can seen the category slug etc .
Please let me know if still needs problem.
Thanking.
I finally fixed a variation of this same issue.
This issue was occurring with category archive pagination.
Initially thought it was within the Loop - turns out the theme I was working on had these filters in a file, custom.php, which manually rewrote the permalink structure.
add_filter('category_link', 'themename_change_archive_link', 100, 2);
add_action('init', 'themename_archive_rewrite', 50);
Pagination didn't like that due to what looks like a conflict with a recent WooCommerce update, so I removed these filters, used the Custom Permalink plugin to rewrite the category permalinks, and it worked! Now I have a custom permalink structure AND functioning category pagination.

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.

WP Multiple Loops - Most Recent Post & Pagination

I'm hoping someone can help me figure this one. It's been giving me problems for a bit. I'm attempting to have a custom category page display the most recent post at the beginning of the page, and below in a separate loop I wish to display the rest of the posts in the given category WITH pagination, offsetting the loop by 1. I've searched numerous ways to do this and can't quite come up with a solution! Any help you can give for this one I'd be soooo grateful for! Thank you in advance! Here's my code:
<?php query_posts( 'cat=3&showposts=1&offset=0'); ?>
<?php while (have_posts()) : the_post(); ?>
<div class="firstPost">
Snipped post style
</div>
<div class="prevHeader">Previous Episodes</div>
<?php endwhile; ?>
<div class="container archiveContainer">
<?php
// set the "paged" parameter (use 'page' if the query is on a static front page)
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
// the query
$the_query = new WP_Query( 'cat=3&posts_per_page=6&offset=1&paged=' . $paged );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php
// the loop
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<div class="prevEntries">
Snipped Post Style
</div>
<?php endwhile; ?>
</div>
<div style="width:100%">
<span style="display:inline-block; margin-left:10px; float:left" class="nav-previous"><?php next_posts_link( '<h4>Older posts</h4>', $the_query->max_num_pages ); ?></span>
<span style="display:inline-block; margin-left:10px; float:right" class="nav-next alignright"><?php previous_posts_link( '<h4>Newer posts</h4>' ); ?></span></div>
<?php
// clean up after the query and pagination
wp_reset_postdata();
?>
<?php endif; ?>
This can certainly be done, but surely not with query_posts. It a case like this, it will outright fail. Also, offsets breaks pagination and are quite difficult to work with if you don't know what you are doing
I don't have time to code now, but here is an idea:
If you need to display the latest post on all your pages, then you can do the following.
Remove you custom query and your offset so that everything works normal. Make use of the default loop and paginate as normal
To display your first post on every page, create a custom query with WP_Query and place it where you need to display this post .See the link on how to properly construct that query. Please do not use query_posts
You will now see that you have the first post displayed twice. To counter this, wrap your custom query in a is_paged() condition.
EDIT
You can try something like this. (PS! showposts is depreciated, use posts_per_page. Also, you can removed the paged parameter when setting an offset as it will be ignored)
(CAVEAT Untested)
if( is_paged() ) {
$args = array(
'posts_per_page' => 1,
'cat' => 3,
);
$q = new WP_Query( $args );
if( $q->have_posts() ) {
while( $q->have_posts() ) {
$q->the_post();
// Display your loop elements
} //end while
wp_reset_postdata();
} // end if have_posts()
} //end if is_paged
// Now for your main loop
// set the "paged" parameter (use 'page' if the query is on a static front page)
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$the_query = new WP_Query( 'cat=3&posts_per_page=6&paged=' . $paged );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
// Your loop elements
}
wp_reset_postdata();
}
Codex states : Specifying hard-coded offsets in queries can and will break pagination since offset is used by WordPress internally to calculate and handle pagination.
See the following link. As described on the page you'll need to use a hook and filter.

Categories