WordPress.org

Forums

Getting a simple list of categories and authors, WITHOUT links! (7 posts)

  1. jayp
    Member
    Posted 10 years ago #

    I have a confession. I'm in love with WordPress. I've been searching all my life for my one true love. I found her a little over a year ago and got married to her this summer. But now there's another. Her name is WordPress. She's not as sexy and not as important, but I love her none-the-less.

    That said, I finally found something that I apparently couldn't do easily with wordpress.

    I want to get a list of categories back, and a list of authors. Just a simple list that I could loop over. I did not want the categories and authors to have links back to their respective pages. I wanted this to display a list of the rss feeds that were available on a site I'm setting up for some friends.

    Here's the result that I finally got to work after half a day of time.

    http://www.xenospiza.com/rssfeeds/

    I'm posting because I want to show my failed attempts, and then show my solution, and particularly, get feedback if this is a good way to do it, if it's efficient, and is there an easier way to do this?

    I tried searching like crazy. The Codex, WordPress Support, and even Google. I could find nothing. All the WordPress functions I saw for return a list of authors and categories brought them back as links to their pages. I just didn't like that and thought it might get the user confused. Wanting an RSS feed but clicking on a link and getting taken somewhere else...

    I also didn't like the way I couldn't easily show that feeds were available in 3 formats. All the native WordPress functions that could spit back links to RSS feeds seemed to only show RSS 2.0.

    So here's the code that I did.

    < p >Category feeds:< /p >
    < ul >
    < ?php //get list of categories

    $cats = @$wpdb- >get_results("
    SELECT DISTINCT wp_categories.cat_ID, wp_categories.cat_name, wp_categories.category_nicename
    FROM wp_post2cat
    LEFT JOIN wp_categories
    ON wp_post2cat.category_id = wp_categories.cat_ID
    LEFT JOIN wp_posts
    ON wp_post2cat.post_id = wp_posts.id
    WHERE wp_posts.post_status='publish' AND wp_categories.category_parent=0
    ORDER BY wp_categories.cat_name
    ");
    if($cats) : foreach($cats as $cat) : start_wp();
    ? >

    < li >< ?php echo $cat- >cat_name? > - < a href="http://www.xenospiza.com/category/< ?php echo $cat- >category_nicename? >/feed/" >RSS 2.0< /a >
    &nbsp; &nbsp; &nbsp;< a href="http://www.xenospiza.com/category/< ?php echo $cat- >category_nicename? >/feed/rss/" >RSS .92< /a >
    &nbsp; &nbsp; &nbsp;< a href="http://www.xenospiza.com/category/< ?php echo $cat- >category_nicename? >/feed/atom/" >ATOM 0.3< /a >< /li >

    < ?php endforeach; endif; ? >
    < /ul >

    < p >Author feeds:< /p >
    < ul >
    < ?php //get list of authors
    $auths = @$wpdb- >get_results("
    SELECT DISTINCT wp_users.ID, user_firstname, user_lastname, user_nicename
    FROM wp_posts
    LEFT JOIN wp_users
    ON post_author = wp_users.ID
    WHERE post_status='publish'
    ORDER BY user_firstname
    ");
    if($auths) : foreach($auths as $auth) : start_wp();
    ? >

    < li >< ?php echo $auth- >user_firstname.' '.$auth- >user_lastname ? > - < a href="http://www.xenospiza.com/author/< ?php echo $auth- >user_nicename? >/feed/" >RSS 2.0< /a >
    &nbsp; &nbsp; &nbsp;< a href="http://www.xenospiza.com/author/< ?php echo $auth- >user_nicename? >/feed/rss/" >/feed/rss/" >RSS .92< /a >
    &nbsp; &nbsp; &nbsp;< a href="http://www.xenospiza.com/author/< ?php echo $auth- >user_nicename? >/feed/atom/" >/feed/atom/" >ATOM 0.3< /a >< /li >

    < ?php endforeach; endif; ? >
    < /ul >

    Writing these queries was excruciating. I had to be able to select only categories with published posts, and only authors with published posts. Turns out, for the categories, I have to do a three way join to get the information I need.

    Is there a better way? Is this way efficient enough? The queries seemed to get executed very quickly in mysql, though my DB is nearly empty.

    Finally, I had to change three core files (Oh, I hate changing core files) to get the feeds to have useful titles...

    In wp-atom.php, wp-rss.php, and wp-rss2.php, I had to change the title tag from


    < title > < ?php bloginfo_rss('name'); ? > < /title >

    to


    < title >
    < ?php bloginfo_rss('name') ? >
    < ?php //custom mod (see original title above)
    if(is_category()) {
    echo " - " . wp_specialchars(get_the_category_by_id($cat));
    } elseif (is_author()) {
    $curauth = get_userdata($author);
    echo " - " . wp_specialchars($curauth- >user_firstname.' '.$curauth- >user_lastname);
    } ? >
    < /title >

    Could the powers that be change the feed pages to include useful titles for what the feed is serving? If your tracking a page, a category, and author (anything else?), it just isn't very cool to have all the feeds with only the site name as their title...

    Feedback loved and appreciated!

  2. jayp
    Member
    Posted 10 years ago #

    i'm going to bump this...

    Can anyone comment?

  3. TechGnome
    Moderator
    Posted 10 years ago #

    You know, instead of hacking the core files (for shame!) you could have just as easily written it as a plugin.....

    -tg

  4. jayp
    Member
    Posted 10 years ago #

    The only hacks to the core files were the rss files, to change the title. The other code I wrote was in the template itself!

    I hate to say this... Though I can cobble together a little bit of code (especially if I keep trying long enough), I don't consider myself the greatest coder in the world, don't write PHP code regularly, and don't know much about how to write wordpress plugins. I guess I'll have to figure that out some day. For now, I was just happy to have most of this stuff incorporated into a theme, since that should be mostly compatible with future releases. (If I have to go back and fix the titles on the rss feeds, that won't take me too long).

    Can anybody else point me in the right direction? I was especially interested in knowing if getting a list of categories and authors (without links) wasn't possible with WordPress's native functions and if I'd taken a good approach with my own solution.

  5. kdmurthy
    Member
    Posted 10 years ago #

    Cant you just use list_cats and use PHP to pull out the category names? That way it will not depend on the database structure also and might be forward compatible.

    Just my 2 cents.

  6. jayp
    Member
    Posted 10 years ago #

    You're saying use PHP to parse what's getting returned from list cats and strip out the links? Could I still loop over it to format things how I want?

    I'll have to look into this

  7. kdmurthy
    Member
    Posted 10 years ago #

    That is what I am suggesting. I would have really preferred wordpress supporting category_loop, author_loop etc. (similar to post loop) and providing list_cats, wp_list_cats on top of those as helper functions. I don't know much about how WP development take place, I am willing to take this up if the developers so wishes.

    In the absense of that, I suggest we use published interfaces rather than accessing database directly.

Topic Closed

This topic has been closed to new replies.

About this Topic