Category Archives: The WEB

WWW

WordPress category list

In answer to the Question: How do I get a list of categories in a WP blog

It’s easily answered with a little Groovy

// Question: How to list all categories in WP blog?
// WP = wordpress

// Solution: quick bit of Groovy

def url = "http://lexecorp.com/"

@Grab(group='org.ccil.cowan.tagsoup',
      module='tagsoup', version='1.2' )
def tagsoupParser = new org.ccil.cowan.tagsoup.Parser()
def slurper = new XmlSlurper(tagsoupParser)
def htmlParser = slurper.parse(url)

htmlParser.'**'.findAll{ it.@href =~ 'category'}.each {
    println it
}

// or with number of posts as well
//htmlParser.'**'.findAll{ it.@class =~ 'cat-item cat-item'}.each {
//    println it
//}

0

which for the URL used, this site, gives

Announcements
Business and Casual Dining
Consulting
Data, Databases & Documentation
Embedded and mobile phone platforms
General
GEO
Graphics, Visualisation, Image Processing
Mind Map
Networking and protocols
Nihongo
Process and practice
Project Euler
Software Design, Development & Programming
The WEB
The Windows platform
Translation and proof-reading
Travel
UNIX and Linux platforms
Result: 0

handy tip of the day!