• jezthomp

    (@jezthomp)


    Whats the best way if at all to choose what order posts are displayed?

    For example a portfolio site and you want your portfolio pieces in a set order, not the order of oldest at the bottom newest most recent…?

    Anyway this can be done…?

    Cheers 🙂

Viewing 3 replies - 1 through 3 (of 3 total)
  • MichaelH

    (@michaelh)

    To resort the order of posts, using the WordPress Default theme for example, in the wp-content/themes/default/index.php file, just before the line:
    <?php if (have_posts()) : ?>

    put this:

    <?php query_posts($query_string . '&orderby=date&order=ASC'); ?>

    The query_posts() article explains the arguments in detail.

    or if you are doing your own query:

    <?php
    $cat_id = get_cat_ID('portfolio');
    $args=array(
      'cat' => $cat_id,
      'orderby'=>'date',
      'order'=> 'ASC',
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'List of Posts';
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    Related:
    Stepping Into Template Tags
    Stepping Into Templates
    Template Hierarchy

    Thread Starter jezthomp

    (@jezthomp)

    Thankyou Michael

    How do i determine what order they go in though, put a number in a custom write panel, or custom field…something like that?

    MichaelH

    (@michaelh)

    Sorry, missed you didn’t want Ascending…

    You could use custom fields and query_posts has a custom field argument to do that

    $args=array(
      'cat' => $cat_id,
      'orderby'=>'meta_value',
      'order'=> 'ASC',
      'meta_key' => 'your_custom_field_key_here'
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );

    Or a plugin:

    http://wordpress.org/extend/plugins/astickypostorderer/

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Best way to custom display posts?’ is closed to new replies.