GrassflatsNighttime On River CityDrawing OnePrint 2StormbringerU!Rapids OneNew York PathGuitarsIcevineTurn On Your LovelightSevere Storm TwoPrint 3A Day Without BloodMeteorsDeep In The WoodsGreen LandFire WallFeel Like A StrangerJam

Wordpress Stuff: Wordpress List Categories (wp_list_categories) Hack

If you’re a web designer/programmer like I pretend to be and you also use Wordpress, you eventually discover that, though it’s an awesome weblog platform, Wordpress suffers from inconsistently and poorly constructed functions that may hinder the implementation of your design goals.

Two such functions involve category construction. My categories are listed horizontally almost midway down the “top of the fold” section. In order to achieve my desired design, I had to hack two Wordpress files. Here’s what I did:

Open wp-includes/category-template.php

On line 277 find

'echo' => 1, 'depth' => 0

and replace with

'echo' => 1, 'depth' => 0, 'before' => '<li>', 'after' => '</li>',
'link_before' => '', 'link_after' => ''

These additions to the wp_list_categories function will increase your design flexibility so that, when your style variable is set to 0 or none, you can specify what comes before and after the anchor element and what comes before and after the text that is contained within the anchor element.

Next, we have put those variable somewhere. Open wp-includes/classes.php

On lines 625 and 626 find

$link .= '>';
$link .= $cat_name . '</a>';

and replace with

if ( 'list' == $args['style'] )
$link .= '>' . $cat_name . '</a>';
else
$link .= '>' . $link_before . $cat_name . $link_after . '</a>';

On line 678 find

$output .= "$link<br />";

and replace with

$output .= $before . $link . $after;

So, to get this working, to one of your theme display files (such as header.php or sidebar.php) add

<?php
wp_list_categories('style=0&link_before=» &before=<p>&after=</p>&link_after= «');
?>

The code that is outputted is

<p><a href="/path_to/category">» Category «</a></p>

That’s it! Don’t be afraid to play around with it and add your own variables.

Will's AvatarPosted by Will on Wednesday, April 30, 2008, at 7:46 pm and filed under Wordpress Stuff.
  1. 1.
    Brendan

    Stumbled on this in a Google search…big help! thank you for it.

    one thing though…what is the purpose of the \n and \t? i copied your text exactly (had to change the single quotes to plaintext, that’s the only exception), but it rendered both of those as text in the document. I took em out with no trouble, but I wanna know if I’m missing out on something!

    thanks again! this tag was a headache.

  2. 2.
    Will

    Hi Brendan,

    Those are unnecessary and just make your source code look pretty. \n begins a new line and \t inserts a tab. So ‘1\n\t2\n\t3\n4′ would give you something like:

    1
    tab-|2
    tab-|3
    4

  3. 3.
    Brendan

    ah, thanks.

  4. 4.
    Justin from SEO Zombie

    Thats actually a nice little hack. I love custom coding wordpress designs, and I agree that some of their functions tie your hands. Nice suggestion.

  5. 5.
    Will

    Thanks, Justin!

  6. 6.
    pass over

    nice hack bro :0

  7. 7.
    Sammy The Killer

    That’s a really great little trick.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Ambiance
Artist Spotlight
Book Reviews
Bug of the Day
Commentary of the Day
Culture
Deep Thoughts
Dumb America
Economics
Election '08
Face of the Day
George Likes Squirrels
Important Thoughts on Holidays
In Their Own Words
Internet Stuff
Literaricals
Movie Reviews
Music of '08
My Research
Occupation of Iraq
Otherwise
Photo-post
Police State
Politics
Psychology
Quote of the Day
Religion
Slow News Day
Still-post
Sunday Funnies
Tech Reviews
Technology
Television
That Time I Quit Smoking
The First Post
Wordpress Stuff

WP Cumulus Flash tag cloud by Roy Tanck requires Flash Player 9 or better.