SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Customizing wp_list-categories() [Wordpress Question]

    Hello,

    I posted this issue on the Wordpress.org forums but the help seems to be lacking - people are offering me links to plugins which I think it a bit OTT for something so simple...

    Currently wp_list_categories gives me a list like this:

    Code html4strict:
    <ul>
      <li><a href="category">Category</a></li>
      <li><a href="category">Category</a></li>
      <li><a href="category">Category</a></li>
    </ul>

    What I want (for styling purposes) is for each anchor to have a span within like this:

    Code html4strict:
    <ul>
      <li><span><a href="category">Category</a></span></li>
      <li><span><a href="category">Category</a></span></li>
      <li><span><a href="category">Category</a></span></li>
    </ul>

    I don't want to edit any of the includes (within "/wp-includes") because doing so would wipe out my change when I upgrade wordpress in the future...

    Any help is appreciated...

    In fact, what would be awesome is if I could have a loop like the one for posts ... something like this:

    Code PHP:
    foreach($categories as $category):
    // My custom stuff here
    endforeach;

    I only wish it were that simple.
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,448
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)
    If you only want the span so you can style the cat links, you should be able to do it using the right CSS selector, no hacking or plugin needed. For example, the cat links in my blog look like
    HTML Code:
    <li class="cat-item cat-item-5">
    <a href="http://www.mittineague.com/blog/category/scripting/"
     title="View all posts filed under Scripting">Scripting</a>
    </li>
    so I can style them with
    Code:
    .cat-item a {color: #f00; }
    It's the same for your blog
    HTML Code:
    <li class="cat-item cat-item-14">
    <a href="http://enhance.qd-creative.co.uk/category/accessibility/"
     title="View all posts filed under Accessibility">Accessibility</a>
    </li>

  3. #3
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm okay with styling everything, but unfortunately I need an extra element to work with so I can apply the style required. The link has to have rounded corners and I cannot use the surrounding list-item because I want the entire rounded box to be clickable...

    I honestly I am quite surprised the Wordpress team haven't made it easier to do this... They could have made it a parameter for wp_list_categories()! ... Oh well.
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  4. #4
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't worry, someone helped me solve it...

    Here it is:

    Code PHP:
    <?php global $wpdb;
        $cat_list = $wpdb->get_results(" SELECT t.name,tt.term_id,tt.count FROM $wpdb->term_taxonomy as tt, $wpdb->terms as t WHERE tt.term_id = t.term_id AND tt.count > 0 AND tt.taxonomy = 'category' ORDER BY t.name");
        foreach ($cat_list as $category ) {?>
            <li><a href="<?php echo get_category_link($category->term_id); ?>"><span><?php echo $category->name; ?></span></a></li>
    <?php } ?>
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  5. #5
    SitePoint Addict
    Join Date
    Mar 2006
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would avoid rolling your own SQL statement to help ensure future compatibility. Use the get_categories function instead.
    PHP Code:
    $cat_list get_categories(); 

  6. #6
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @aaron Thanks! That seems work perfectly.

    Is it possible for me to do the same for "archives"? ... "get_archives()" does not seem to work.
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •