Reverse the way of function output

2019-08-10 08:40发布

问题:

I am having a hard time reversing this loop.

Atm, the loop is showing the newest posts first, because these are added on top of the others in an array i guess, whereas i want it to show the oldest first.

So my question is, how to make the oldest posts show first, and the newest show at the end?

This is the code which shows the newest posts first, in a while loop:

while($portfolio->have_posts())
{
    $portfolio->the_post();
    $post = get_post();
    $output .= '<a class="w-portfolio-item-anchor" href="javascript:void(0);" data-id="'.$post->ID.'">'
}
return $output;

Regards, Patrick

回答1:

To reverse this, add this to your theme somewhere, just before your loop:

query_posts($query_string . "&order=ASC");

Or find where it is currently being queried for and ensure that &order=ASC is used.

Or, as @ɴ-ᴀ-ᴛ-ʜ mentioned in a comment, change this:

$output .= '<a class="w-portfolio-item-anchor" href="javascript:void(0);" data-id="'.$post->ID.'">'

To this:

$output = '<a class="w-portfolio-item-anchor" href="javascript:void(0);" data-id="'.$post->ID.'">' . $output;


标签: php wordpress