Getting a simple list of categories and authors, WITHOUT links!
-
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.
https://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="https://www.xenospiza.com/category/< ?php echo $cat- >category_nicename? >/feed/" >RSS 2.0< /a >
< a href="https://www.xenospiza.com/category/< ?php echo $cat- >category_nicename? >/feed/rss/" >RSS .92< /a >
< a href="https://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="https://www.xenospiza.com/author/< ?php echo $auth- >user_nicename? >/feed/" >RSS 2.0< /a >
< a href="https://www.xenospiza.com/author/< ?php echo $auth- >user_nicename? >/feed/rss/" >/feed/rss/" >RSS .92< /a >
< a href="https://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!
- The topic ‘Getting a simple list of categories and authors, WITHOUT links!’ is closed to new replies.