Wordpress Pagination wp_query - php

I have a custom page which lists ALL of the posts no matter the category, I have hit a brick wall with the pagination! for some reason the pagination ins't showing up!
Here is my code
<?php // Template Name: News Feed ?>
<?php get_header(); ?>
<div id="content">
<div class="container">
<div class="row">
<div class="col-md-3">
<div id="left-sidebar">
<?php get_sidebar('posts') ?>
</div>
</div>
<div class="col-md-6">
<?php
$args = array(
'post_type' => 'post',
'orderby' => 'date',
'order' => 'DESC',
);
$my_query = new WP_Query($args);
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<div class="post">
<h2><?php the_title(); ?></h2>
<small><?php the_time('F jS, Y'); ?> by <?php the_author_posts_link(); ?></small>
<div class="entry">
<?php the_excerpt(); ?>
</div>
<p class="postmetadata"><?php _e( 'Posted in' ); ?> <?php the_category( ', ' ); ?></p>
</div>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft"><?php previous_posts_link('Previous Entries'); ?></div>
<div class="alignright"><?php next_posts_link('Next Entries'); ?></div>
</div>
</div>
<div class="col-md-3">
<?php get_sidebar(); ?>
</div>
</div>
</div>
</div>
<?php get_footer(); ?>

Your links will not show up because next_posts_link() is set to the $max_num_pages parameter of the main query ($wp_query->max_num_pages) by default. On pages, this will always be 1 and by default these links don't display when there is only one page
Also, you are not paginating your query, so even if you gt your links working, you will see the same posts being repeated.
You should add the paged parameter to your query like this
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
'paged' => $paged,
// Rest of you arguments
);
and then you need to alter the $max_pages parameter in next_posts_link() like this
next_posts_link( 'Next entries', $my_query->max_num_pages );

First try to add 'posts_per_page' parameter into the $args, so the loop will know how much posts to display in the page.
Second,From WordPress Codex :
If the pagination is broken on a static front page you have to add the "paged" >parameter this way:
<?php
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }
query_posts('posts_per_page=3&paged=' . $paged);
?>
That means Just add this code before your args (without the "query_posts") and then but the args like this :
$args = array(
'post_type' => 'post',
'paged' => $paged,
'orderby' => 'date',
'order' => 'DESC',
I am recommending to explore the codex once again, maybe there is something there that can help you.
http://codex.wordpress.org/Pagination#Example_Loop_with_Pagination

You have created custom template so you should try with the following link first:
https://wordpress.org/support/topic/pagination-not-working-on-custom-template-1
Also, I request you to go through this link and it explain everything in details
By default, in any given context, WordPress uses the main query to determine pagination. The main query object is stored in the $wp_query global, which is also used to output the main query loop:
if ( have_posts() ) : while ( have_posts() ) : the_post();
When you use a custom query, you create an entirely separate query object:
$custom_query = new WP_Query( $custom_query_args );
And that query is output via an entirely separate loop:
if ( $custom_query->have_posts() ) :
while ( $custom_query->have_posts() ) :
$custom_query->the_post();
But pagination template tags, including previous_posts_link(), next_posts_link(), posts_nav_link(), and paginate_links(), base their output on the main query object, $wp_query. That main query may or may not be paginated. If the current context is a custom page template, for example, the main $wp_query object will consist of only a single post - that of the ID of the page to which the custom page template is assigned.
If the current context is an archive index of some sort, the main $wp_query may consist of enough posts to cause pagination, which leads to the next part of the problem: for the main $wp_query object, WordPress will pass a paged parameter to the query, based on the paged URL query variable. When the query is fetched, that paged parameter will be used to determine which set of paginated posts to return. If a displayed pagination link is clicked, and the next page loaded, your custom query won't have any way to know that the pagination has changed.
https://wordpress.stackexchange.com/questions/120407/how-to-fix-pagination-for-custom-loops
I hope this will work for you

$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; // setup pagination
$the_query = new WP_Query( array(
'post_type' => 'post',
'paged' => $paged,
'posts_per_page' => 5)
);
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<div>' . get_the_title() . '</div>';
the_content();
endwhile;
echo '<nav>';
echo '<div>'.get_next_posts_link('Older', $the_query->max_num_pages).'</div>'; //Older Link using max_num_pages
echo '<div>'.get_previous_posts_link('Newer', $the_query->max_num_pages).'</div>'; //Newer Link using max_num_pages
echo "</nav>";
wp_reset_postdata(); // Rest Data
Would you please try above code?

Related

How do add the blog next prev navigation?

I use custom post type for my journal post but I want to add navigation for previous and next page. So If I have more than 5 post on my journal page, I will page next link or the number 2,3,4, etc... If there is no more post, it should display previous. Right now, my journal page in on the index.php .
And in my wordpress reading settings, I use a static page for my front page displays. My front page is front-page.php and my posts page is journal page. Blog pages show at most 5 posts. Syndication feeds show the most recent 5posts.
How do I add next and previous navigation using my custom post type "journals"?
<?php
get_header();
?>
<!-- journal -->
<section class="container-wrap">
<?php
$args = array('post_type' => 'journals');
$query = new WP_Query($args);
while($query -> have_posts()) : $query -> the_post();
?>
<article class="post-wrap">
<header>
<a href="<?php the_permalink(); ?>" class="post-title">
<h1 class="post-title"><?php the_title(); ?></h1>
</a>
<span class="post-date"><?php echo(types_render_field('date', array('format' => 'm.d.Y') )); ?></span>
</header>
</article>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</section>
<!-- /journal -->
<?php
get_footer();
?>
Use the paginate_links hook in combination with a custom WP_Query array. Make sure to specify the paged array parameter for the query. This sets up the query to give back paged results.
<?php
// 1- Setup paging parameter
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
// 2- Setup WP_query variable to get last 12 posts
$args = array(
'posts_per_page' => 12,
'post_type' => 'journals',
'orderby' => 'most_recent',
'paged' => $paged,
);
$the_query = new WP_Query( $args );
// 3- Setup new loop
if($the_query->have_posts()) :
while($the_query->have_posts()) :
$the_query->the_post();
// 4- Output parameters you want here
echo '<div class="col-md-4">';
echo '<h4>' . the_title() . '</h4>';
echo '' . the_post_thumbnail() . '';
echo the_excerpt();
echo '</div>';
// 5- close up loop
endwhile;
endif;
// 6- Output paginate_links just below post loop
echo paginate_links( array(
'base' => str_replace( 999999, '%#%', esc_url( get_pagenum_link( 999999 ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $the_query->max_num_pages
) );
// 7- reset post data query
wp_reset_postdata();
?>
Try this my code it's working on my site http://www.thehiddenwhy.com/blog/ please this link How to create pagination in page code in wordpress?

posts per page on a static page + pagination

Thanks for taking the time to check this out.
I've spend a couple days going over this. Lots of time on wp.org but i'm just not getting it. Should be an easy fix I'm sure. No matter what i try, i can't limit the number of posts per page nor get any pagination to display. This is my most recent attempt (maybe not my best attempt). The page just shows all the posts OR all the recent ones or something (not my site). Once i can at least get the page to limit the posts, then i'll tackle the pagination. Also, setting the posts per page in the WP dashboard does nothing...and never did. That's why i'm trying to code something myself. Why can't i limit the number of posts per page? Would i put the pagination where i currently have it? Is this a total mess, lol?
Thanks again,
Dave (code below)
<?php /* Template Name: Stories */ ?>
<?php
get_header(); ?>
<!-- *************************************** -->
<div class="custom_two_third">
<?php
// The Query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('posts_per_page' => 3, 'paged' => $paged );
$the_query = new WP_Query($args);
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
get_template_part( 'template-parts/content', get_post_format() );
}
// pagination
next_posts_link();
previous_posts_link();
} else {
get_template_part( 'template-parts/content', 'none' );
}
/* Restore original Post Data */
wp_reset_postdata();
?>
<div class="clear"></div>
</div><!-- custom_two_third -->
<?php
get_sidebar();
get_footer();
?>
Try using post_type
$args_articles =array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 5,
'order' => 'desc',
'paged' => $paged
);
// the query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$news_eve_args = array( 'post_type' => 'news_events', 'posts_per_page' => 4, 'paged' => $paged);
$wp_query = new WP_Query( $news_eve_args );
if ( $wp_query->have_posts() ) : ?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post();?>
<?php the_title(); echo "<br/>"; ?>
<?php endwhile; ?>
<nav>
<?php previous_posts_link('« Newer',$wp_query->max_num_pages); ?>
<?php next_posts_link('Older »',$wp_query->max_num_pages); ?>
</nav>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no news or events at this time.', 'theme' ); ?></p>
<?php endif; ?>
Try this one, it will work in your case.
global $query_string;
query_posts("{$query_string}&posts_per_page=12");
while (have_posts()) {
the_post();
...
}

After edit child categories, wordpress show post in wrong categories

I had a categories with subcategories. After reorganize all categories, remove the subcategories and move post from some categories to other categories (with Bulk Move plugin), some categories in category.php show wrong content, mixing post from two categories.
For example, category "mistery" show post from "history, mistery". If I search one of these post that are showing incorrect in two categories, the assigned category is correct at wordpress backend
Other categories show posts correctly.
I think that this error it is happening with old subcategories.
Can somebody help me? Thank you.
This is my actual code loop in categories.php
<!-- loop de post normales-->
<?php
wp_reset_query();
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$category = get_the_category($post->ID);
$category = $category[0]->cat_ID;
query_posts(array(
'post_type' => 'post', // You can add a custom post type if you like
'paged' => $paged,
'posts_per_page' => 9,
'category__in' => array($category)
));
?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<article class="col-sm-6 col-lg-6 bottomMargin27">
<a class="noLine" href="<?php the_permalink();?>">
<?php the_post_thumbnail('fb') ?>
<div class="articleContainer">
<h4 class="articleTitle"><?php the_title(); ?></h4>
</div>
</a>
</article>
<?php endwhile; ?>
<?php wp_pagenavi(); wp_reset_query(); ?>
<?php else : ?>
<?php endif; ?>
<!-- / fin del loop de post normales -->
Try rewriting the query like this
query_posts(
array(
'post_type' => 'post',
'paged' => $paged,
'posts_per_page' => 9,
'cat' => $category // This is new
));

How to add posts in wordpress pages?

I want to show some posts in the homepage of my wordpress site..How will I do it??or is there any plugin that can help me do it?or is there shortcodes that could pull out and display those post in my homepage?
If you want to do it the easy way, you can use a plugin like Display Posts Shortcode.
Or, if you want to do it manually, you can use get_posts().
Here's an example you could use:
<?php
if (is_page()) {
$cat=get_cat_ID($post->post_title); //use page title to get a category ID
$posts = get_posts ("cat=$cat&showposts=5");
if ($posts) {
foreach ($posts as $post):
setup_postdata($post); ?>
<?php the_title(); ?></h2>
<?php endforeach;
}
}
?>
I hope this helps!
The implemetation varies greatly by theme.
Check if your wordpress theme has a file called index.php.
If you have this file for your current theme, this is the file responsible for displaying your home page. And this is where you will have to put the code snippets to display posts.
Presuming that you know a bit of html and PHP you will have to decide the suitable place within index.php to add the code suggested above by Amal Murali.
If you want to show a specific category post on Homepage you can use category slug or category name. Like the below to show and use the wp_pagenavi() plugin to show pagination and present it.
<?php
$paged = (get_query_var( 'paged' )) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => 'CATEGORY NAME ',
'posts_per_page' => 5,
'paged' => $paged,
);
$arr_posts = new WP_Query( $args );
if ( $arr_posts->have_posts() ) :
while ( $arr_posts->have_posts() ) :
$arr_posts->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php
if ( has_post_thumbnail() ) :
the_post_thumbnail();
endif;
?>
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
</header>
<div class="entry-content">
<?php the_excerpt(); ?>
Read More
</div>
</article>
<?php
endwhile;
wp_pagenavi(
array(
'query' => $arr_posts,
)
);
endif;
?>

Wordpress (3.4.2) Pagination Issue - Custom Post Type

I've ran into a problem where I can't seem to get pagination to work on the latest wordpress version 3.4.2. I have developed a lot of wordpress sites so this rather confusing. I've got a custom post type of news, I wish to show 8 posts per page and the page url would be '/news'. This page displays correctly with the right pagination at the bottom (using WP-pagenavi). When I click on the second page (/news/page/2) I get my 404 page. I'd really appreciate any help on resolving this.
<?php
/*
Template Name: News
*/
?>
<?php get_header(); ?>
<div class="container_12">
<div class="grid_12 box">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'news', 'posts_per_page' => 8, 'paged' => $paged);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="news">
<h4><?php the_title() ?></h4></a>
<span><?php the_time('F jS, Y'); ?></span>
<p><?php limit_excerpt("30"); ?></p>
</div>
<?php endwhile; ?>
<div id="pagination">
<?php if(function_exists('wp_pagenavi')) {
wp_pagenavi( array(
'query' =>$loop
));
}
?>
</div>
</div>
</div><!-- .container_12 -->
<?php get_footer(); ?>
[Update - Solution]
So I've coded a solution for this. Put this in the functions.php
//Set for cpts
function cpt_init( $cpt )
{
add_rewrite_rule(
"{$cpt}/page/([^/]+)/?",
"index.php?pagename={$cpt}&pag=$matches[1]",
'top' );
}
//Only Add Once
add_filter( 'query_vars', 'cpt_query_vars' );
function cpt_query_vars( $query_vars )
{
$query_vars[] = 'pag';
return $query_vars;
}
//Add for each type
add_action( 'init', 'cpt_init', 'news' );
And in your loop use get_query_var('pag') instead of get_query_var('page')
Did you try using global variable $paged instead of attributing it your own value ?
This is for example what is used in a cutom theme :
<?php
global $paged;
$the_query = new WP_Query();
$the_query->query('post_type=post&posts_per_page=14&post_status=publish&paged='.$paged);
?>

Categories