go41

How to sort the result of a query, latest posts reverse

von Joern am 21. Jul. 2009 | Keine Kommentare

How to take a wordpress query and display the resulting 4 posts in reverse order?

Let me give you an example:

In the theme Arthemia is on the top right side a section called ‚Featured‘, which queries for the 4 latest posts of the category named Featured.

These posts are displayed normally with the latest post on top, followed by older posts of this category.
To reverse this order, there is a handy WordPress Function called array_reverse(). This function takes the array of a query and reverses the order of posts.

Most times it is used to display your comments in a reverse order like this:

Code:

<?php $comments = array_reverse($comments, true); ?>

But we can use this function to reverse any array of posts, not only comments.

To query the posts we have to use get_posts with a foreach loop instead of query_posts or the regular WP loop with if (have_posts()) : while (have_posts()) : the_post();
I give you an example code of the original arthemia query for the featured posts:

Code:

<div id="featured">

   <img src="<?php echo get_option('home'); ?>/wp-content/themes/arthemia/images/featured.png" width="72px" height="17px" alt="" />

   <?php query_posts("showposts=4&category_name=Featured"); $i = 1; ?>

         <?php while (have_posts()) : the_post(); ?>
   <div class="clearfloat">
   <?php $values = get_post_custom_values("Image");
   if (isset($values[0])) { ?>
      <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
   <img src="<?php echo bloginfo('template_url'); ?>/scripts/timthumb.php?src=/<?php
$values = get_post_custom_values("Image"); echo $values[0]; ?>&w=100&h=65&zc=1&q=100"
alt="<?php the_title(); ?>" class="left" width="100px" height="65px"  /></a>
      <?php } ?>
   <div class="info"><a href="<?php the_permalink() ?>" rel="bookmark" class="title"><?php the_title(); ?></a>
<div class="meta">[<?php the_time('j M Y') ?> | <?php comments_popup_link('No Comment', 'One Comment', '% Comments');?> | <?php if(function_exists('the_views')) { the_views(); } ?>]</div>   

</div>
       </div>

      <?php endwhile; ?>

   </div>

Exactly his query_posts above with a while (have_posts() loop we replace completely! with the following code to reverse the order of the same posts we had before:

Code:

<div id="featured">

   <img src="<?php echo get_option('home'); ?>/wp-content/themes/arthemia/images/featured.png" width="72px" height="17px" alt="" />

   <?php $postslist = get_posts("numberposts=4&category_name=Featured");
$reverseposts = array_reverse( $postslist, true);
foreach ($reverseposts as $post) : setup_postdata($post); ?>

   <div class="clearfloat">
   <?php $values = get_post_custom_values("Image");
   if (isset($values[0])) { ?>
      <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
   <img src="<?php echo bloginfo('template_url'); ?>/scripts/timthumb.php?src=/<?php
$values = get_post_custom_values("Image"); echo $values[0]; ?>&w=100&h=65&zc=1&q=100"
alt="<?php the_title(); ?>" class="left" width="100px" height="65px"  /></a>
      <?php } ?>
   <div class="info"><a href="<?php the_permalink() ?>" rel="bookmark" class="title"><?php the_title(); ?></a>
<div class="meta">[<?php the_time('j M Y') ?> | <?php comments_popup_link('No Comment', 'One Comment', '% Comments');?> | <?php if(function_exists('the_views')) { the_views(); } ?>]</div>   

</div>
       </div>

      <?php endforeach; ?>

   </div>

What is different, you might ask? It’s actually only the query on top

Code:

<?php query_posts("showposts=4&category_name=Featured"); $i = 1; ?>
<?php while (have_posts()) : the_post(); ?>

replaced by:

Code:

<?php $postslist = get_posts("numberposts=4&category_name=Featured");
$reverseposts = array_reverse( $postslist, true);
foreach ($reverseposts as $post) : setup_postdata($post); ?>
and at the end of the loop is

<?php endwhile; ?>

replaced by

Code:

<?php endforeach; ?>

happy coding, if you get problems just join the forum and ask here
____________________
you find me on Google+, Twitter and Facebook

(von: Joern)

Hier noch 1 weitere Ergebnisse dieses Threads:

WP 3.2 visual post editor broken

8. Jul. 2011 (von: Joern)

All sites on my server had the problem that in 'Edit Post' under All Posts to edit a single post or page the button 'Visual' didn't work. The drop down 'Screen Options' and the dropdown to logout also didn't work.In…

Autor:

Du findest mich auch auf Twitter und Facebook!

Schreibe einen Kommentar

Pflichtfelder sind mit * markiert.


Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.

weitere forum Beiträge: