hide posts";
}
else {
if ($definitions) {
$showPostsQueryString .= "&" .$definitionsQueryString;
}
$postsLink = "show posts";
}
GLOBAL $wpdb;
$catNameArray = array();
$catNameQuery = "SELECT cat_name, category_nicename
FROM wp_categories
WHERE wp_categories.category_count > 0";
$catNameResults = $wpdb->get_results("$catNameQuery");
foreach ($catNameResults as $catNameRow) {
$catNameItem = array();
$catNameItem["cat_name"] = $catNameRow->cat_name;
$catNameItem["category_nicename"] = $catNameRow->category_nicename;
$catNameArray[] = $catNameItem;
} // foreach
function linkDesc ($string, $catNameArray, $oneCatID, $thisRelUri) {
if (count($catNameArray) < 1)
return $string;
$baseUrl = "#cat-";
if ($oneCatID) $baseUrl = $thisRelUri ."?cat=";
foreach ($catNameArray as $catNameItem) {
$search = $catNameItem["cat_name"];
$replace1 = "";
$replace2 = "";
$string = preg_replace("/($search)([\"'\s,\.\-_:\/=\q\!\?\(\)<>~\[\]])/i", $replace1 ."\\1" .$replace2 ."\\2", $string);
} // foreach
return $string;
} // end function linkDesc
$html = "
Categories ($postsLink):"; // may be overwritten if one category
// one category
$oneCatID = FALSE.
$cat = "";
$catWhere = "";
if (isset($_GET["cat"])) {
$cat = trim($_GET["cat"]);
if ($cat) {
$oneCatID = TRUE;
$catWhere = " WHERE wp_categories.category_nicename = '$cat' ";
$show_posts = TRUE;
$html = "";
}
} // got category slug
$catQuery = "SELECT * FROM wp_categories $catWhere ORDER BY cat_name";
$catResults = $wpdb->get_results("$catQuery");
foreach ($catResults as $catRow) {
$cat_ID = $catRow->cat_ID;
$cat_name = $catRow->cat_name;
$category_nicename = $catRow->category_nicename;
$category_description = $catRow->category_description;
$category_parent = $catRow->category_parent;
$category_count = $catRow->category_count;
$cat_link_count = $catRow->link_count;
$cat_no_of_posts = 0;
$postHtml = "";
// get and count posts
/* ! lazy approach, optimize it for trafficked pages by using the post counter in the category table when $show_posts is false ! */
$postQuery = "SELECT wp_post2cat.*,
wp_posts.ID,
wp_posts.post_title,
wp_posts.post_name,
wp_posts.comment_count
FROM wp_post2cat
LEFT OUTER JOIN wp_posts ON (wp_posts.ID = wp_post2cat.post_id)
WHERE wp_post2cat.category_id = $cat_ID
AND wp_posts.post_status = 'publish'
AND wp_posts.post_type = 'post'
ORDER BY wp_posts.post_date DESC";
$postResults = $wpdb->get_results("$postQuery");
foreach ($postResults as $postRow) {
$post_ID = $postRow->ID;
$post_title = $postRow->post_title;
$post_name = $postRow->post_name;
$comment_count = $postRow->comment_count;
// change if you've different permalinks:
$post_url = "http://$blogLocaction/" .$post_name ."/";
$cat_no_of_posts = $cat_no_of_posts + 1;
$postHtml .= "- $post_title";
if ($comment_count > 0) {
if ($comment_count > 1) {
$postHtml .= " ($comment_count comments)";
}
else {
$postHtml .= " ($comment_count comment)";
}
}
$postHtml .= "
\n";
} // foreach posts of category
if (!empty($postHtml)) $postHtml = "\n";
if ($cat_no_of_posts > 0) {
$catUrl = get_category_link($cat_ID);
/*
http://example.com/wp-rss2.php?cat=42
or
http://example.com/category/categoryname/feed
*/
// redirects to feedburner: $catFeedUrl = "http://$blogLocaction/wp-rss2.php?cat=" .$cat_ID;
$catFeedUrl = $catUrl ."feed";
$catName = $cat_name;
if (empty($catName)) $catName = $category_nicename;
$catTitleText = trim(strip_tags($category_description));
$catTitleText = str_replace("\"", "'", $catTitleText);
$catDesc = linkDesc($category_description, $catNameArray, $oneCatID, $thisRelUri);
$catUCName = strtoupper($catName) ."
";
$catDomId = "cat-" .strtolower($category_nicename);
if ($oneCatID) {
$html .= "- List all categories with links to all posts
\n";
}
if ($oneCatID && $catTitleText) {
$html .= "- $catUCName$catDesc
\n";
}
if ($oneCatID) {
$oneCatLink = "";
}
else {
$oneCatUrl = $thisRelUri ."?cat=" .strtolower($category_nicename);
$oneCatLink = " ($catName overview) ";
}
if ($definitions) {
$prnDefinition = "$catUCName$catDesc
";
}
$html .= "- $prnDefinition$catName $oneCatLink
$cat_no_of_posts posts.\n";
if ($show_posts) $html .= $postHtml;
$html .= " \n";
} // cat has posts
} // foreach categories
if ($oneCatID) $html .= "
\n";
else $html .= "
\n";
if (!stristr($blogLocaction, "sebastians-pamphlets")) {
// you can suppress this link when you link out to my blog under credits or whereever.
$html .= "Sitemap courtesy of Sebastian
\n";
}
print $html;
?>