Reverse order of posts not working - wordpress

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

Related

How to fix pagination link to page 2 wordpress?

Hi all I am a new of wordpress I have problame with pagination, I used plugin Wp-pagnavi ,When I click link to page 2 it same as page 1,How to to fix it.
You can see it at
http://westecmedia.com/?page_id=758
And this my code in page-event.php
<?php
/*
* Template Name: Page - Events Page
*/
?>
<?php get_header(); ?>
<div id="content-events">
<div id="head-event"><h3>EVENTS</h3></div>
<div id="main-event">
<?php query_posts('category_name='.get_the_title().'&post_status=publish,future');?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="part-event">
<div id="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<div id="event-dess">
<h2><?php the_title(); ?></h2>
<p>
<?php
$content = get_the_content();
$content = strip_tags($content);
echo substr($content, 0, 300);
?>
</p>
<div id="read-more">Read More</div>
</div>
</div>
<div id="line-bottom"></div>
<?php endwhile; endif; ?>
<?php wp_pagenavi(); ?>
<?php wp_reset_query(); ?>
</div>
</div>
<?php get_footer(); ?>
Help me please :(
Include paged, documentation: https://codex.wordpress.org/Pagination
<?php
$args = array(
'cat' => '5',
'post_type' => 'post',
'posts_per_page' => 6,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
);
query_posts($args);
while (have_posts()) : the_post();
/* Do whatever you want to do for every page... */
endwhile;
wp_pagenavi();
wp_reset_query(); // Restore global post data
?>
Also don't use query_posts to fetch data in WordPress, consider using https://codex.wordpress.org/Class_Reference/WP_Query.
Please ask WordPress related question here: http://wordpress.stackexchange.com
I hope this helps.

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.

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

Wordpress pagination with static pages

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

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.

Resources