A few months back, I posted my techniques for creating Author Archives. Now with the advent of MT2.2 and the MySQL backend, this can be automatic instead of having to be edited when an author is added.
Part 1: Author List
Part 2: Author Archives
The files described below don't need to be created inside of MT unless you are using MT tags for formatting outside of the data that you are trying to capture.
(special thanks goes to Brenna for much of the Author List code, as well as help on the Archives. The connect.php file was her idea as well. Lynda came up with the PHP to make authors show/hide on the Archives page. And PHP.net was invaluable in this!)
Setup for MySQL connection file
When working with MySQL databases, you need to tell your script to connect to the database first. And so you don't need to have this information on every MySQL page you work with, you can create a connection file that stores this and call on it every page you work with.
Each persons configuration of their server might be a bit different but here's what mine looks like:
<?
// name of your database
$database = "database_name";
// connect to database
$db=mysql_connect ("localhost", "user_name", "password") or die ('I cannot connect to the database.');
mysql_select_db ("database_name");
?>
When you set up your MySQL database, you have a database (mine's love_mt), username (love_kristine), and password, and those are the things you'll fill in above. If your server is setup differently, you might be able to check the database section of your cpanel/control panel and see if it has a connection string in the section where you set up your database.
Part 1: Author List
Now that you have a connection file, you can use it at the top of your file to create an author list. I have 4 pieces of info that I want to display – Author name, Author email, Author URL, and then the Author name is used to point to the "about" page for each author.
<?php
//connection info
include ("/home/USERNAME/public_html/PATHTO/connect.php");
//We want to make a list of all of the authors who have permission to post in one blog – for me, this is blog_id=3, theredkitchen. So we join the tables for mt_author and mt_permission to get this information. Each of the pieces below are table names and row names.
$listauth = "SELECT a.author_id, a.author_name, a.author_email, a.author_url FROM mt_author a, mt_permission p WHERE (p.permission_author_id = a.author_id) and (p.permission_blog_id = 3)";
$result = mysql_query($listauth);
while ($row = mysql_fetch_array($result)) {
//Here's where I tell it what I want to show. You can see how each row name is an item that was SELECTed above.
echo "<b>", ($row['author_name']), ": </b>", "<a href=\"", ($row['author_name']), ".html\">about</a> <a href=\"mailto:", ($row['author_email']), "\">email</a> <a href=\"", ($row['author_url']), "\">homepage</a><br />\n";
}
mysql_free_result($result);
?>
The results of that query are shown on my About the Cooks page on TRK.
Part 2. Author Archives
This was a bit tricker, and I spent a whole afternoon trying to figure out how to put a query inside of a query. I want to display a list of every entry that an author has made, with a link to the individual entry for that post. I also want to filter it out so I can use a URL like http://theredkitchen.net/authors.php?author=AUTHORNAME to only show the posts by that Author.
<?php
//connection info
include ("/home/love/public_html/mt/templates/connect.php");
//first, create a list of authors to be clickable to show one author at a time on the page.
echo "<b>Quick links – click to show recipes</b>:<br>";
//this is the same query as the Author List, but without selecting as many terms out of the database.
$authorarray = "SELECT a.author_name FROM mt_author a, mt_permission p WHERE (p.permission_author_id = a.author_id) and (p.permission_blog_id = 3)";
$resulta = mysql_query($authorarray) or die (mysql_error());
while ($rowa = mysql_fetch_array($resulta)) {
echo "<a href=\"?author=", ($rowa['author_name']), "\">", ($rowa['author_name']), "</a> | \n";
}
echo "<br>";
//Now get more author information to put on the header of each query. This works as a list of authors so that the query below it knows how many authors to get the entry information from, and what their names are.
$authorlist = "SELECT a.author_id, a.author_name, a.author_url, a.author_email FROM mt_author a, mt_permission p WHERE (p.permission_author_id = a.author_id) and (p.permission_blog_id = 3)";
$result = mysql_query($authorlist) or die (mysql_error());
while ($row = mysql_fetch_array($result)) {
//This works along with the show one author piece above.
if ($_GET['author'] == $row['author_name']) {
//This is the header for each section of the authors.
echo "<div class=\"blog\"><a name=\"", ($row['author_name']), "\"></a><div id=\"banner\">", ($row['author_name']), "</div><div align=\"right\"><a href=\"", ($row['author_name']), ".html\">about</a> <a href=\"", ($row['author_url']), "\">home</a> <a href=\"mailto:", ($row['author_email']), "\">email</a></div>\n";
//Now, for each author, select the entries that they have published. We are pulling out the Title, ID, Authorname, Date, and Blog ID for blog_id=3 and the author from the above query.
$authorentry = "SELECT entry_title, entry_id, entry_author_id, entry_created_on, entry_blog_id FROM mt_entry WHERE (entry_blog_id = 3) and (entry_author_id = (".$row['author_id']."))";
$results = mysql_query($authorentry) or die (mysql_error());
while ($rows = mysql_fetch_array($results)) {
//Because my entries are saved in an archive filename like this – <$MTArchiveDate format="%Y/%m"$>/<$MTEntryID pad="1"$>.php – I had to pull the date out of the entry_created_on to point to the proper directory.
$year = substr($rows['entry_created_on'], 0, 4);
$month = substr($rows['entry_created_on'], 5, 2);
//and here is where I put my link together for it to display a link to each recipe by the author.
echo "<a href=\"", "http://theredkitchen.net/oldrecipes/", $year, "/", $month, "/00", ($rows['entry_id']), ".php\">", ($rows['entry_title']), "</a><br>\n";
}
echo "</div>";
}
}
?>
The results of this query are displayed at the Author Archive on theredkitchen.
If you need help with this, please feel free to post back. I'll try my best to help you out, even though this is quite new to me!