Wordpress pagination with static pages - wordpress

I am new to wordpress. I am using my blog category to show all the posts as static pages. Every thing is fine except the pagination. When i googled i found that it's a known bug in wordpress.Here is my page.php code:
<div id="primary">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!-- #primary -->
I am using list_category_post plugin for pagination but it not working with static pages. Please help me ?

When I want to show posts on the static page, I generate the query myself.
I add something like this before while loop:
<?
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args=array(
'posts_per_page' => 5,
'paged' => $paged
);
$wp_query = new WP_Query($args);
?>
after the loop I would have navigation links:
<div id="nav-below" class="navigation">
<div class="nav-previous"><?php next_posts_link(); ?></div>
<div class="nav-next"><?php previous_posts_link(); ?></div>
</div><!-- #nav-below -->
and after that I would add wp_reset_query() to reset the original query, just in case some other code needs it.
<?php wp_reset_query(); ?>

Related

Pagination For Custom Query Is Not Working

I am creating a custom homepage template in which I am trying to show posts with navigation.
Here is what I have followed.
Created a file homepage.php and gave the template name as "HomePage".
Created a page "Home" from dashboard and assigned this template "HomePage".
Then from settings > reading, chosen static front page "Home".
Here is my code for querying posts.
<div class="posts-container">
<?php query_posts('post_type=post&posts_per_page=2&post_status=publish&paged='. get_query_var('paged')); ?>
<?php if(have_posts()): ?>
<?php while (have_posts() ) : the_post(); ?>
<div class="post">
<h4 class="entry-title"><?php the_title(); ?></h4>
</div>
<?php endwhile; // end of the loop. ?>
</div>
<div class="pagination-nav">
<div class="alignleft"><?php next_posts_link(__('Next »','example')); ?></div>
<div class="alignright"><?php previous_posts_link(__('« Previous','example')); ?></div>
</div>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
Now, on clicking the links generated by pagination functions, I am getting redirected to the page where I was first. While the URL gets updated like
http://localhost/mysite/?paged=2
Lets say if there are 4 posts (post 1, post 2, post 3, post 4) that and the recent ones are post 4 and post 3 respectively, then I cannot get to post 1 and post 2. Both of the pages
http://localhost/mysite
http://localhost/mysite/?paged=2
show me only the post 4 and post 3. What can I do to solve this?
For a static front page you need to check for the page var too, in this way:
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('...&paged=' . $paged);
This was the solution that worked for me. I had to re-write the query and have used WP Query this time.
<?php
global $wp_query, $paged;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$query_args = array (
'post_type' => 'post',
'post_status'=>'publish',
'posts_per_page' => 4, //for testing purposes
'paged' => $paged,
);
$wp_query = new WP_Query($query_args);
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts()) :
$wp_query->the_post();
?>
<div class="post">
<?php if ( has_post_thumbnail() ) { ?>
<a class="entry-image" href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); } ?>
</a>
<h4 class="entry-title"><a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a></h4>
<div class="entry">
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>
<div class="pagination-nav">
<div class="alignleft">
<?php next_posts_link(__('Oudere artikelen')); ?>
</div>
<div class="alignright">
<?php previous_posts_link(__('Newer')); ?>
</div>
</div>
</div>
<!-- /posts-container -->
<?php endif; ?>
<?php wp_reset_postdata(); ?>
And Whola!, this one worked.
Might be useful for someone else.

WordPress Latest News Page Showing Custom Fields From All News Stories

I have my own custom WordPress theme and I am building out the 'Latest News' section at the moment. I have a custom field for my page header image and alt tag for each page on my site, I am using the following code to display those custom fields on each page:
<section class="page-heading">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<img src="<?php the_field( 'header_image' ); ?>" alt="<?php the_field( 'main_header_image_alt_tag' ); ?>">
<?php endwhile; endif; ?>
</section>
Which works fine. But on my 'Latest News' page all the custom fields from all the news stories (posts) are being displayed, so for example if I have 3 post displayed per page then I am getting 3 images and 3 alt tag displayed.
In my settings I have my 'Posts Page' set to 'Latest News'.
Is there a way I can only have 1 image and 1 alt tag displayed and not all 3?
Thanks.
EDIT
I've added an image to better explain... The images that span the screen are from the news stories and not the custom field for the 'Latest News' page.
I don't know if I understand your question, but you want your first post show the image and the rest not?
I guess you have to write something like this:
<section class="page-heading">
<php $show_first_image = true; ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if($show_first_image): $show_first_image = false; ?>
<img src="<?php the_field( 'header_image' ); ?>" alt="<?php the_field( 'main_header_image_alt_tag' ); ?>">
<?php endif; ?>
<?php endwhile; endif; ?>
I think I understand your problem, your latest news is also a Page (post type), which has the header image.
Your index file/archive is like this:
<?php
get_header();
if(have_posts()):
while(have_posts()):
// your latest news * 3.
endwhile;endif;
get_footer(); ?>
the problem is, got its items loaded before the get_header(), where your header image will be showed.
What you do is something like this:
<?php
global $wp_query;
$original_query = $wp_query;
$wp_query = null;
$page = get_query_var( 'pagename' );
$wp_query = new WP_Query( array('post_type' => 'page', 'pagename' => $page ) );
get_header(); // in the get_header() you will probably have the <section class="page-heading">
$wp_query = null;
$wp_query = $original_query;
if(have_posts()):
while(have_posts()):
// your latest news * 3.
endwhile;endif;
get_footer(); ?>
I hope this is your answer to your question.
I got around this by creating a new template for my news page and using WP_Query inside that template to bring in the latest news stories. I also unset 'Posts Page' in my reading settings as that's what was causing all the issues.
I'm not sure if that is the most efficient way of doing things but it worked for me.
VVC, I appreciate your help and effort.
<!-- WP Query Arguments -->
<?php
$args = array(
'page_id' => '103'
);
$the_query = new WP_Query( $args );
?>
<!-- End WP Query Arguments -->
<!-- WP Query loop -->
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php endwhile; ?>
<?php else: ?>
<h2>No posts to display</h2>
<?php endif; ?>
<!-- End WP Query loop -->
<!-- WP Query Arguments -->
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => '5'
);
$the_query = new WP_Query( $args );
?>
<!-- End WP Query Arguments -->
<!-- WP Query loop -->
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="news-story">
<div class="news-heading">
<h5><?php the_title(); ?></h5>
<p class="publish-date"><strong>Posted on:</strong> <?php the_time('j M, Y'); ?></p>
</div>
<?php the_excerpt(); ?>
<p>Full Story <i class="fa fa-angle-double-right"></i></p>
</div>
<?php endwhile; ?>
<?php else: ?>
<h2>No posts to display</h2>
<?php endif; ?>
<!-- End WP Query loop -->

Trouble with Pagination - Not displaying

I am using Twitter Bootstrap 3 and the Roots Theme.
i have a page called 'portfolio' that displays 6 portfolio items out of a total of 15,
I am using the following code:
/proman/assets/img/code011.jpg" alt="Folio Feature Image">
<!-- Add the pagination functions here. -->
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$query_recents = new WP_Query ( array( 'post_type' => 'portfolio', 'posts_per_page' => 3, 'paged' => $paged ) );
if ( $query_recents->have_posts() ):
?>
<!-- Start of the main loop. -->
<?php while ( $query_recents->have_posts() ) : $query_recents->the_post(); ?>
<div class="col-sm-4">
<!-- the rest of your theme's main loop -->
<?php get_template_part('templates/folio', get_post_format()); ?>
</div>
<?php endwhile; ?>
<!-- End of the main loop -->
<!-- Add the pagination functions here. -->
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
<?php else : ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
but nothing displays at all. I have looked at the codex as I was told this doesnt work for single page templates , Ive tried all kinds of varieties but nothing will display.
This has worked for me previously in a none bootstrap environment, Im not sure what I need to do.
would appreciate if anyone has an answer.
Do you have your variable $paged defined?
If not, add above your code
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
I'm also using roots and bootstrap 3 with a portfolio while integrating the .thumbnail component.
check here: http://calebserna.com/portfolio/
It's working great. My solution was to create a category 'portfolio' then edit 'templates/content.php'.
<?php if(is_category('192')) : ?>
<div
<?php post_class('col-xs-6 col-md-3'); ?> >
<!-- bootstrap 3 thumbnails component -->
<div class="thumbnail">
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('bootstrap_portfolio_thumb'); ?>
</a>
<?php } ?>
<div class="caption">
<?php the_title(); ?>
</div>
</div>
</div>
<?php else : ?>
<?php
//the default content loop below
then edit config.php so a sidebar does not appear in category 'portfolio'.
if you really want to use a custom page with wp_query try inserting the defaul roots paging code in your template
//index.php
<?php if ($wp_query->max_num_pages > 1) : ?>
<nav class="post-nav">
<ul class="pager">
<li class="previous"><?php next_posts_link(__('← Older posts', 'roots')); ?></li>
<li class="next"><?php previous_posts_link(__('Newer posts →', 'roots')); ?></li>
</ul>
</nav>
<?php endif; ?>

Pagination for custom post type on page template

Hello I am trying to add pagination to a page template which I am using to display a custom post type. I have looked around for a few hours but I haven't come across clear set of instructions to follow.
Below is all the code from my page template php file. If someone could point me in the right direction it would be much appreciated.
I would appreciate any help on how to add the code for
<?php
/*
Template Name: Testimonials
*/
?>
<?php get_header(); ?>
<div class="container_12">
<div id="content" class="grid_8">
<h1><?php the_title(); ?></h1>
<div id="testimonials">
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $testimonials = new WP_Query( array( 'post_type' => 'testimonial', 'orderby' => 'title', 'order' => 'ASC', 'posts_per_page' => '2', 'paged' => $paged) ); ?>
<?php if (have_posts()) : ?>
<ul>
<?php while ($testimonials->have_posts()) : $testimonials->the_post(); ?>
<li>
<?php the_content(); ?>
<span><?php the_title(); ?></span>
</li>
<?php endwhile; ?>
</ul>
<?php else : ?>
<p><?php _e('No entry found.'); ?></p>
<?php endif; ?>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('« Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries »') ?></div>
</div>
</div><!-- #testimonials -->
</div><!-- #content -->
<?php get_sidebar(); ?>
</div>
<?php get_footer(); ?>
I'm able to access the page 2 with direct link testimonials/page/2/. But on page 1, the navigation does not appear.
Page 1:
Page 2:
You should reset your post data after WP query, as I think.
Check StackExchange people answers to this
https://wordpress.stackexchange.com/questions/89191/using-query-posts-inside-single-php-loop
Good luck
Try using query_posts() instead and see if this solves the problem.

Reverse order of posts not working

Here's my HTML for an archive page:
<?php get_header(); ?>
<nav>
<?php wp_nav_menu(array('menu' => 'Main Nav Menu')); ?>
</nav>
<div id="main-content-archive">
<h5 class="inner_text_shadow">Archive</h5>
<div id="clear-box">
<?php query_posts( array(
'posts_per_page' => 16,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ),
));
?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<?php $bg_image = post_thumb( get_the_post_thumbnail() );?>
<a href="<?php the_permalink() ?>"><div class="post-bg-archive" style="background: #777777 url(<?php echo $bg_image;?>);">
<div id="title-bg">
<div class="transparency"></div>
<div class="archive-title"><h98><?php the_title(); ?></h98></div>
</div>
</div></a> <!-- END post-bg -->
</div>
<?php endwhile; ?>
<?php else : ?>
<h2>Not Found</h2>
<?php endif; ?>
</div> <!-- END post_class function -->
<div id="archive-nav"><h12><?php include (TEMPLATEPATH . '/inc/nav.php' ); ?></h12></div>
</div> <!-- END main-content -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
It's not actually an archive page btu modified front page that has more posts showing and a grid view.
The only thing missing here is the reverse order of posts. I want my archive to start with the oldest post. I've read all over and found a PHP code for reversing order but I can't seem to input it correctly.
This is the code that I used:
<?php query_posts($query_string . "&order=ASC"); ?>
Depending on where I put it, it either:
messes up something and I get a PHP error
does nothing
instead of showing a 4x4 grid, it shows 1 box that leads to localhost/archive i.e. the page that I'm on already.
I also tried resetting query and using the aforementioned code after that but that gave me the 3rd version of the problem (also mentioned above).
So, how to get a reverse order of posts at this page?
Try this code:
<?php query_posts( array(
'posts_per_page' => 16,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 )).'&order=ASC');?>

Resources