Wordpress Pagination on static homepage - php

I am trying to get Pagination working on my static homepage which I have integrated with wordpress. The problem I am having is when I click the "Older Entries" button on the page it goes to the ?paged=2 page but displays the first 10 posts still. Just like on the first page.
I know the code somewhat works because I changed ($the_query->max_num_pages > 1) to 2 and went to the page and it displayed the contents of the second page with a "Newer Entries" button.
I just cannot get it to do it automatically.
I used this persons tutorial on how to set it up
http://callmenick.com/post/custom-wordpress-loop-with-pagination
and here is my code
<?php
// set up or arguments for our custom query
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$query_args = array(
'post_type' => 'post',
'paged' => $paged
);
// create a new instance of WP_Query
$the_query = new WP_Query( $query_args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); // run the loop ?>
<article>
<h1><?php echo the_title(); ?></h1>
<div class="excerpt">
<?php the_excerpt(); ?>
</div>
</article>
<?php endwhile; ?>
<?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 } ?>
<?php else: ?>
<article>
<h1>Sorry...</h1>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
</article>
<?php endif; ?>
The website (temporary) can be seen here http://auroraservers.org/MainSite/Index.php
So you can see what it is doing.
Thank you for any help! I've been trying to fix this for over a day now so I hope it isn't too much trouble.

This line should be paged instead of page
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;

Related

Pagination in wordpress show the same post

I have a realy big problem with pagination. When i click next post - wordpress show me the same 9 post. What is bad with this? This is meybe on $row array? I need custome post view, and pagination.
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 9,
'paged' => $paged
);
$query = new WP_Query ( array( $args ) );
$row = array(6, 3, 3, 4, 4, 4, 3, 3, 6);
$i = 0;
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id ( $the_query->ID ), 'thumbnail' );
?>
<div class="col-md-<?php echo $row[$i] ?> col-xs-6">
<div class="blog-item scrollpoint sp-effect2">
<div class="cover" style="background-image: url('<?php echo $thumbnail[0] ?>')">
<div class="mask">
<div class="post">
<span class="kategoria"></span>
<div class="tresc">
<h1> <?php the_title(); ?></h1>
<div class="wypis"><?php echo get_the_excerpt(); ?></div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
$i++;
endwhile; ?>
<!-- pagination -->
<?php previous_posts_link('« Newer posts') ?>
<?php next_posts_link('Older posts »') ?>
<?php wp_reset_postdata();?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
The previous_posts_link and next_posts_link are designed to show the next or former post from the currently displayed post. You're displaying a loop.
If, as this seems, you are just making a blog index page and requesting regular posts, then you just need to make links that append /{paged}/ to the end, like so:
Next
<?php if($paged > 1): ?>
Previous
<?php endif;?>
Where blogpage is the page this is supposed to appear (if the home, just do / )
If you're doing something more complicated, like a custom post type with a custom permalink structure, look into query_vars and the Rewrite API.

how to do this with wordpress API and php

I am really a newbie in Wordpress and php. I created a theme for my website as follows:
I think my question is clear: in wordpress admin page I have four categories: 1,2,3,4. I want last 2 posts in category 1 to be displayed in section (div) 1 (shown above), last 2 posts in category 2 to be displayed in section (div) 2, and so on.
As I said, I am novice and I faced a tons of functions in Wordpress documentation.
for example I used the code below inside index.php (of my custom theme) in section 2 which outputs: "Sorry, no posts matched your criteria."
<?php
$args = array( 'post' => 'post', 'posts_per_page' => 1,'category_name'=>'تازه ها');
$the_query = new WP_Query( $args );
?>
<?php
if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<div class="entry-content">
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif;
?>
Please give a piece of code to do this.
This should work:
$posts = get_posts ("cat=1&showposts=2");
if ($posts)
{
foreach ($posts as $post):
setup_postdata($post); ?>
<h2><?php the_title(); ?></h2>
<?php endforeach;
}
Change cat=1 to the id of each of the four categories.

Wordpress single.php second query pagination

I created a single.php Wordpress template file to show standard WP Posts.
This is the truncated version of the code:
<?php
// First (Main Content) Loop
if (have_posts()) :
while (have_posts()):the_post();
?>
<div class="entry-content">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
<?php
// Second (Related Posts) Loop
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args=array(
'category__in' => 3,
'posts_per_page'=> 4,
'paged' => $paged
);
$temp = $wp_query;
$wp_query= null;
$wp_query = new wp_query($args);
if( $wp_query->have_posts() ) :
while ($wp_query->have_posts()) :
$wp_query->the_post();
?>
<div class="entry-content">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
<?php
endwhile;
echo '<div id="navigation">'.get_next_posts_link('Load More').'</div>';
endif;
}
$wp_query = null;
$wp_query = $temp;
?>
The 4 related posts are generated as expected and the get_next_posts_link generates a link to /post-name/page/2.
I'm using Paul Irish's Infinite Scroll plugin to load page/2's posts into the container (Which is working fine on the archive- and index.php pages on the site).
Unfortunately when you click get_next_posts_link the same 4 posts are appended to the container.
Any ideas how to get this to work?
I've tried a lot of other solutions on StackOverflow but so far none have worked.
Any help appreciated, cheers.

exclude category from wordpress post

I want to exclude category from shoowing my blog posts. My category id is 62. category name is perfect_work
Here is my wordpress blog template code:
<div id="left" class="eleven columns">
<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('paged='.$paged);
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<div class="title">
<h2><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title(); ?>" ><?php the_title(); ?></a></h2>
<div class="postmeta"> <span>by <?php the_author_posts_link(); ?></span> | <span><?php the_time('l, F jS, Y') ?></span> | <span><?php the_category(', '); ?></span> </div>
</div>
<div class="entry">
<?php $image_attr = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'top_feature'); ?>
<img src="<?php echo $image_attr[0]; ?>" class="postim scale-with-grid" id="blog-thumb" >
<?php wpe_excerpt('wpe_excerptlength_archive', ''); ?>
<div class="clear"></div>
</div>
</div>
<?php endwhile; ?>
<?php getpagenavi(); ?>
<?php $wp_query = null; $wp_query = $temp;?>
</div>
I already tried using
$wp_query = new WP_Query('cat=-62');
its not work. I also put
<?php query_posts('cat=-62'); ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
Its work but page navigation not work, and also not showing others post. only 1st 5 post show.
Any Solution?
Get the page number
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
Then you may use
$wp_query = new WP_Query('cat=-62&paged=' . $paged);
Or use
$cat_id = get_cat_ID('perfect_work');
$wp_query = new WP_Query('cat=-' . $cat_id . '&paged=' . $paged);
Then loop
if($wp_query->have_posts()) :
while ($wp_query->have_posts()) : $wp_query->the_post();
// ...
endwhile;
endif;
Try this one you have to specify the showposts to limit the posts
<?php $wp_query->set( 'cat', '-62' ); ?>
<?php query_posts( 'showposts=10' ); ?>
<?php if( have_posts() ) : ?>
<?php while( have_posts() ) : the_post(); ?>
.
.
.
<?php endwhile; ?>
<?php endif; ?>
Note : The minus sign indicates the exclusion of all Posts which
belong to that category from being retrieved from the database. In
turn, the Loop will never have Posts of that category id and only
process the specified number of Posts of other category ids.
Please read the codex on WP_Query, it is imo very detailed, look at the category params part
Just add a minus sign - in front of the categories you dont want, so the below code would mean show posts with category 10 and 11, but exclude category 62
$recent = new WP_Query("showposts=3&cat=10,11,-62")
You don't need to use the $temp variable before or after the query. You should use something like this:
//This should do the trick
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'cat' => -62,
'paged' => $paged
);
// the query
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
//The real trick!
<?php wp_reset_postdata(); ?>
Two things to note:
The paged query parameter
To reset the query use wp_reset_postdata()

Pagination in custom loop not working

I have already searched several topics and questions regarding this problem, but I haven´t found any answer that suits my code.
The pagination for my custom posts is displayed, but when I click in Show next posts >> the same posts from the previous page are shown, even though the URL shows ?paged=2.
My code is the following:
<div class="podcast-entries">
<?php
$args = array( 'post_type' => 'strn5_podcasts',
'posts_per_page' => 3,
'paged' => (get_query_var('paged') ? get_query_var('paged') : 1 );
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query( $args );
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) :
$wp_query->the_post();
?>
<div <?php post_class(); ?> >
<h4 class="podcast-title"><?php the_title(); ?></h4>
<div class="podcast-content">
<?php the_content(); ?>
</div>
<h6><?php the_time('F j, Y'); ?></h6>
<div class="post-separator col-lg-12"></div>
</div>
<?php endwhile; endif; ?>
<div class="navigation-links">
<div class="next-post">
<?php next_posts_link( '>> Siguiente Entrada' ); ?>
</div>
<div class="previous-post">
<?php previous_posts_link( 'Anterior Entrada >>' ); ?>
</div>
</div>
<?php $wp_query = null;
$wp_query = $temp;
wp_reset_query(); ?>
</div> <!-- .podcast-entries -->
Usually what I do when something doesn't work is that I strip it from everything and start rebuilding it with bare basics to see what the problem might be.
Why don't you try a simpler form of loop to see if you can get it to prin.t
<?php query_posts(array('posts_per_page' => 3, 'post_type' => 'strn5_podcasts', 'paged' => get_query_var('page'))); if (have_posts()) : while (have_posts()) : the_post(); ?>
<p><?php the_title(); ?></p>
<?php endwhile; endif;?>
<div class="navigation-links">
<div class="next-post"><?php next_posts_link( '>> Siguiente Entrada' ); ?></div>
<div class="previous-post"><?php previous_posts_link( 'Anterior Entrada >>' ); ?></div>
</div>
It's pretty much the same thing you are doing (almost) but without any variables or any other thing that "could" go wrong.
Edit
In the end it turned out that the paged parameter can be used with paged or page and in this case it worked out for you with page instead of paged
So it ended up looking like so:
'paged' => get_query_var('page')
On a final note, using get_query_var('page') is needed if you're using query posts in a custom page template that you've set as your homepage.
I agree with sulfureous. I think more specifically though, these three lines are the problem:
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query( $args );
Based off of your description it seems like this would cause the issue if $wp_query is being caught as not null. I'm not sure why you'd even have that in there but you must have your reason.
At the very least take it out the first two lines of that to help you troubleshoot.

Categories