WordPress.org

Ready to get started?Download WordPress

Forums

(Very) Custom Category List? (5 posts)

  1. DarkRyder
    Member
    Posted 7 years ago #

    Is there any to get the category data as, say, a PHP array rather than dumping it directly to the browser using list_cats or dropdown_cats? I'm creating a new template for my web site and need to be able to list the categories in the following format:

    <p><a href="$url"><img ...>$name</a> ($count, last $date)</p>

    I would very much prefer not to have to output buffer and re-parse the output from list_cats, but I've seen in the Codex that making your own functions which execute SQL is a Bad Thing and all the category-related functions in template-functions-category.php do their own database access.

    Any ideas? Am I missing something obvious?

  2. whooami
    Member
    Posted 7 years ago #

    you can do that without an array, you just need to edit the function. Look at the code, its not all that hard.

    I, for instance, include an image within my <li> tags. I could just as easily make those <p> tags, and the placement of the image could be moved.

    Even using different images wouldnt be that hard, using the category ids as markers for the proper image.

    Again, look at the code.

    Its not all that custom.

  3. DarkRyder
    Member
    Posted 7 years ago #

    Oh, it wouldn't be difficult, I just read somewhere in the plugin docs that you aren't "supposed" to write functions which touch the database. I guess I'll probably end up doing it anyway.

    It seems like it wouldn't be a bad idea to separate out the database code from list_cats and dropdown_cats, anyway. As it stands, if someone calls both functions in the same page (not all that unreasonable, depending on the design), it results in separate database queries for the same data. If the database code were moved to a separate function, say get_cats, the results of the first call could be cached and made available to subsequent calls without additional database queries.

  4. whooami
    Member
    Posted 7 years ago #

    i think youre overcomplicating things.

    I just read somewhere in the plugin docs that you aren't "supposed" to write functions which touch the database.

    Your question was about an array, you STILL have to touch the db to get the data to put into the array. I'm merely telling you that you dont need to write a new function, with minor edits, VERY minor edits, the current fucntion can achieve what you want.

    Good luck!

  5. DarkRyder
    Member
    Posted 7 years ago #

    Sorry, I guess I'm habitually forward-looking when it comes to writing code. ^_^

    In general, I'd prefer to avoid editing the core WP files -- as long as my template is completely self-contained, not only is it suitable for use by others, but I can also upgrade WP in the future without having to worry (as much) about code changes. I'll just borrow the SQL from list_cats and write a function which caches and returns the record list. That way, I can do whatever I like with those categories without having to rewrite the SQL every time I change my mind.

    Thanks for the advice, though!

Topic Closed

This topic has been closed to new replies.

About this Topic