List categories with alphabetical headings

Discussion in 'WordPress' started by Ruriko, Jun 14, 2009.

  1. #1
    I found a code to list my categories in 2 columns but now I want it to show the alphabetical headings for example http://www.zomganime.com/anime-series-list/. So does anyone know how? don't say use a plugin because I'm trying to use less plugins to save performance.
    <?php
    $cats = explode("<br />",wp_list_categories('title_li=&echo=0&depth=1&style=none'));
    $cat_n = count($cats) - 1;
    for ($i=0;$i<$cat_n;$i++):
    if ($i<$cat_n/2):
    $cat_left = $cat_left.'<li>'.$cats[$i].’</li>’;
    elseif ($i>=$cat_n/2):
    $cat_right = $cat_right.’<li>’.$cats[$i].’</li>’;
    endif;
    endfor;
    ?>
    <ul class=”left”>
    <?php echo $cat_left;?>
    </ul>
    <ul class=”right”>
    <?php echo $cat_right;?>
    </ul>
    Code (markup):

     
    Ruriko, Jun 14, 2009 IP
  2. mizaks

    mizaks Well-Known Member

    Messages:
    2,066
    Likes Received:
    126
    Best Answers:
    0
    Trophy Points:
    135
    #2
    Use the "orderby" parameter in the wp_list_categories tag.

    More info on it can be found here.
     
    mizaks, Jun 14, 2009 IP
  3. Ruriko

    Ruriko Well-Known Member

    Messages:
    4,023
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    130
    #3
    The orderby is not what I'm looking for. It needs to show the alphabetical heading, did you have a look at the example? I want the categories to display like this

    A
    apple
    air
    acting
    artist

    B
    blue
    blueberry
    batman
    boy
     
    Ruriko, Jun 14, 2009 IP
  4. mizaks

    mizaks Well-Known Member

    Messages:
    2,066
    Likes Received:
    126
    Best Answers:
    0
    Trophy Points:
    135
    #4
    Sorry, I missed your example. You'll have to use a plugin for that (even though you don't want to). AZIndex is one that will give output like you want.
     
    mizaks, Jun 14, 2009 IP
  5. internetmarketingiq

    internetmarketingiq Well-Known Member

    Messages:
    3,552
    Likes Received:
    70
    Best Answers:
    0
    Trophy Points:
    165
    #5
    Geekly Weekly has a page order and a link order and a category order plugin that works with WP 2.8. You may have to make slight modifications to your Template.. but it's not that hard.
     
    internetmarketingiq, Jun 14, 2009 IP
  6. Ruriko

    Ruriko Well-Known Member

    Messages:
    4,023
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    130
    #6
    I don't want to use a plugin I would want it as a template code
     
    Ruriko, Jun 14, 2009 IP
  7. Gray Fox

    Gray Fox Well-Known Member

    Messages:
    196
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #7
    
    <?php
    $cats = explode("<br/>", wp_list_categories('title_li=&echo=0&depth=1&style=none'));
    $cat_n = count($cats) - 1;
    for ($i=0; $i<$cat_n; $i++) {
        $this_letter = strtoupper(substr(trim(strip_tags($cats[$i])), 0, 1));
        $this_letter = ctype_alpha($this_letter) ? $this_letter : "#"; // Replace '#' with your desired nonalphabetic heading
        if ($i<$cat_n/2) {
            $cat_left .= ($this_letter != $last_letter) ? "<li>{$this_letter}</li>" : "";
            $cat_left .= "<li>{$cats[$i]}</li>";
        } else {
            $cat_right .= ($this_letter != $last_letter) ? "<li>{$this_letter}</li>" : "";
            $cat_right .= "<li>{$cats[$i]}</li>";
        }
        $last_letter = $this_letter;
    }
    ?>
    
    PHP:
    Try this out, contact me via IM if you get any problems
     
    Gray Fox, Jun 16, 2009 IP
  8. Big0ne

    Big0ne Well-Known Member

    Messages:
    2,615
    Likes Received:
    81
    Best Answers:
    0
    Trophy Points:
    165
    #8
    Big0ne, Jun 16, 2009 IP
  9. Ruriko

    Ruriko Well-Known Member

    Messages:
    4,023
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    130
    #9
    Ruriko, Jun 16, 2009 IP