• Howdy

    Not much of a coder (obviously) but was able to modify the following (found) code so that it generates an unordered list which can be popped into a text widget and used in a sidebar, in conjunction with php-exec – works a charm

    Generates an unordered list of all the posts in a current category, great for sectional navigation

    Question – how can I alter this so that it generates an alphabetical list of posts?

    many thx in advance

    /max

    <?php
    if ( is_single() || is_category()) {
      $categories = get_the_category();
      if ($categories) {
        foreach ($categories as $category) {
          // echo "<pre>"; print_r($category); echo "</pre>";
          $cat = $category->cat_ID;
          $args=array(
            'cat' => $cat,
            'post__not_in' => array($post->ID),
            'posts_per_page'=>40,
            'caller_get_posts'=>1
          );
          $my_query = null;
          $my_query = new WP_Query($args);
          if( $my_query->have_posts() ) {
            echo '<h4 class="widget-title">'. $category->name . '</h4><ul>';
            while ($my_query->have_posts()) : $my_query->the_post(); ?>
              <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
             <?php
            endwhile;
          } //if ($my_query)
        } //foreach ($categories
      } //if ($categories)
      wp_reset_query();  // Restore global post data stomped by the_post().
    } //if (is_single())
    ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • I’m unsure if it would work, but based on this page could you try adding 'order'=>'ASC' to your array of arguments?

    Thread Starter rev-max

    (@rev-max)

    Thanks you gave me a good clue/place to google from

    this was what did it!

    <?php
    if ( is_single() || is_category()) {
      $categories = get_the_category();
      if ($categories) {
        foreach ($categories as $category) {
          // echo "<pre>"; print_r($category); echo "</pre>";
          $cat = $category->cat_ID;
          $args=array(
            'orderby' => 'title',
      'order' => 'ASC',
    'cat' => $cat,
            'post__not_in' => array($post->ID),
            'posts_per_page'=>40,
            'caller_get_posts'=>1
          );
          $my_query = null;
          $my_query = new WP_Query($args);
          if( $my_query->have_posts() ) {
            echo '<h4 class="widget-title">'. $category->name . '</h4><ul>';
            while ($my_query->have_posts()) : $my_query->the_post(); ?>
              <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
             <?php
            endwhile;
          } //if ($my_query)
        } //foreach ($categories
      } //if ($categories)
      wp_reset_query();  // Restore global post data stomped by the_post().
    } //if (is_single())
    ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘code chunk generates list of posts for current category, how to alphabetize?’ is closed to new replies.