My site has a Wordpress blog on it, but it does not power the whole site, just the blog. If my entire site were powered by Wordpress, then I could generate a sitemap for Google using Wordpress functions.
I did not want to have Wordpress control everything; I wanted to have more control. By doing some quick queries, you can generate a sitemap for Google that has all of your blog entries in it.
Setting it Up
First, you will need to send the correct headers, create the opening xml tag, and connect to your database.
<?php
header("Content-Type: text/xml;charset=iso-8859-1");
echo '<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
require_once('DB CONNECTION GOES HERE'); //This is where I would require my DB connection file
If you are not sure how to connect to your database, you will need to contact your host to get that information, and then do a search for php MySQL connection. If you can’t figure it out still, let me know, and I can help.
Get the Categories
Next, we want to query the database to get the categories used in Wordpress:
$query = "SELECT cat_ID, category_nicename
FROM wp_categories
ORDER BY category_nicename";
$result = @mysql_query($query);
Then, we need to loop through the categories and display a url entry for each category:
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<url>
<loc>http://trevordavis.net/blog/' . $row['category_nicename'] . '/</loc>
<changefreq>weekly</changefreq>
</url>';
Get the Entries
Next, we need to create a query to return all of the entries for each category:
$artQuery = "SELECT p.post_name, DATE_FORMAT(p.post_date, '%Y-%m-%d') AS createdOn
FROM wp_posts AS p, wp_categories AS cat, wp_post2cat AS pc
WHERE p.ID = pc.post_id AND pc.category_id = " . $row['cat_ID'] . "
GROUP BY p.ID
ORDER BY p.ID DESC";
$artResult = @mysql_query($artQuery);
Finally, we want to create a url entry for each blog entry:
while($artRow = mysql_fetch_array($artResult, MYSQL_ASSOC)) {
echo '<url>
<loc>http://trevordavis.net/blog/' . $row['category_nicename'] . '/'. $artRow['post_name'] . '.php</loc>
<lastmod>'.$artRow['createdOn'].'</lastmod>
<changefreq>weekly</changefreq>
</url>';
}
To finish it off, we just close everything up:
}
echo'</urlset>';?>
The Whole Script
Here is the finished script:
<?php
header("Content-Type: text/xml;charset=iso-8859-1");
echo '<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
require_once('DB CONNECTION GOES HERE'); //This is where I would require my DB connection file
$query = "SELECT cat_ID, category_nicename
FROM wp_categories
ORDER BY category_nicename";
$result = @mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<url>
<loc>http://trevordavis.net/blog/' . $row['category_nicename'] . '/</loc>
<changefreq>weekly</changefreq>
</url>';
$artQuery = "SELECT p.post_name, DATE_FORMAT(p.post_date, '%Y-%m-%d') AS createdOn
FROM wp_posts AS p, wp_categories AS cat, wp_post2cat AS pc
WHERE p.ID = pc.post_id AND pc.category_id = " . $row['cat_ID'] . "
GROUP BY p.ID
ORDER BY p.ID DESC";
$artResult = @mysql_query($artQuery);
while($artRow = mysql_fetch_array($artResult, MYSQL_ASSOC)) {
echo '<url>
<loc>http://trevordavis.net/blog/' . $row['category_nicename'] . '/'. $artRow['post_name'] . '.php</loc>
<lastmod>'.$artRow['createdOn'].'</lastmod>
<changefreq>weekly</changefreq>
</url>';
}
}
echo'</urlset>';
?>
Modifying the .htaccess file
We also want to rewrite the url for this file so that it is available at http://trevordavis.net/sitemap.xml. Open up your .htaccess file, or create one if you don’t have one. Then add the following:
RewriteEngine on
RewriteRule sitemap.xml googleSitemap.php
Upload this file along with the googleSitemap.php script to your site root, and you are set. You can also add the rest of your site’s file structure to the sitemap as well. You can see how I did this in my google sitemap.
Sitemaps Protocal
You can read more about the sitemaps protocal to see what other attributes you can apply to each url entry.
By the Way…
Let me know if you can think of any way to improve this.
Categories
Recent Articles
February 2012
| S | M | T | W | T | F | S |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 |
4 Comments
Ronald Allan
09.08.2007Looks nice this will be a big help on my site… really hard making a sitemap…
tnx
vinny
10.28.2008Greetings from the UK!
I was interested in your xml sitemap article and particularly its ability to generate the blog posts. How would you include the non-dynamic site pages in this sitemap or do you have this as separate. Also, why have the redirect - forgive me if i’m being dumb!!!
Vinny
Trevor
10.28.2008@Vinny-
You could include the static pages right after you open the
<urlset>or before you close it.I was only doing the redirect so that it would live at /sitemap.xml instead of /googleSitemap.php
——-
Iwyhltjs
11.10.2010Three official remix videos exist for this song. , hostory channel.com, hostory channel.com, http://hdhtozrj.guwexur.co.cc/hostory-channel.com.html hostory channel.com, 904,
Too late, comments are closed!
Don’t worry, you can email me or contact me on Twitter.