Latest Post From Each Category');
add_option('ddle_sort', 'cata');
add_option('ddle_cat_links', TRUE);
add_option('ddle_hide_protected', TRUE);
add_option('ddle_show_date', TRUE);
add_option('ddle_date_format', 'F jS, Y');
add_option('ddle_excerpts', FALSE);
add_option('ddle_chars', 200);
add_option('ddle_comment_link', FALSE);
add_option('ddle_comment_num', FALSE);
add_option('ddle_comments_none', FALSE);
add_option('ddle_limit', 0);
add_option('ddle_exclude', '');
add_option('ddle_include', '');
add_option('ddle_max_per_cat', 1);
function ddle_add_option_pages() {
if (function_exists('add_options_page')) {
add_options_page('Latest From Each Category', 'DDLatestFromEach', 8, __FILE__, 'ddle_options_page');
}
}
function ddle_options_page() {
global $ddle_version;
if (isset($_POST['set_defaults'])) {
echo '
';
update_option('ddle_header', 'Latest Post From Each Category
');
update_option('ddle_sort', 'cata');
update_option('ddle_cat_links', TRUE);
update_option('ddle_hide_protected', TRUE);
update_option('ddle_show_date', TRUE);
update_option('ddle_date_format', 'F jS, Y');
update_option('ddle_excerpts', FALSE);
update_option('ddle_chars', 200);
update_option('ddle_comment_link', FALSE);
update_option('ddle_comment_num', FALSE);
update_option('ddle_comments_none', FALSE);
update_option('ddle_limit', 0);
update_option('ddle_exclude', '');
update_option('ddle_include', '');
update_option('ddle_max_per_cat', 1);
echo 'Default Options Loaded!';
echo '
';
} else if (isset($_POST['info_update'])) {
update_option('ddle_header', (string) $_POST["ddle_header"]);
update_option('ddle_sort', (string) $_POST["ddle_sort"]);
update_option('ddle_cat_links', (string) $_POST["ddle_cat_links"]);
update_option('ddle_hide_protected', (bool)$_POST["ddle_hide_protected"]);
update_option('ddle_show_date', (bool) $_POST["ddle_show_date"]);
update_option('ddle_date_format', (string) $_POST["ddle_date_format"]);
update_option('ddle_excerpts', (bool) $_POST["ddle_excerpts"]);
update_option('ddle_chars', (string) $_POST["ddle_chars"]);
update_option('ddle_comment_link', (bool) $_POST["ddle_comment_link"]);
update_option('ddle_comment_num', (bool) $_POST["ddle_comment_num"]);
update_option('ddle_comments_none', (bool) $_POST["ddle_comments_none"]);
update_option('ddle_limit', (string) $_POST["ddle_limit"]);
update_option('ddle_exclude', (string) $_POST["ddle_exclude"]);
update_option('ddle_include', (string) $_POST["ddle_include"]);
update_option('ddle_max_per_cat', (string) $_POST["ddle_max_per_cat"]);
echo 'Configuration Updated!';
echo '';
} ?>
prefix;
$ddle_header = get_option('ddle_header');
$ddle_sort = get_option('ddle_sort');
$ddle_cat_links = get_option('ddle_cat_links');
$ddle_hide_protected = get_option('ddle_hide_protected');
$ddle_show_date = get_option('ddle_show_date');
$ddle_date_format = get_option('ddle_date_format');
$ddle_excerpts = get_option('ddle_excerpts');
$ddle_chars = (int)get_option('ddle_chars');
$ddle_comment_link = (bool)get_option('ddle_comment_link');
$ddle_comment_num = (bool)get_option('ddle_comment_num');
$ddle_comments_none = (bool)get_option('ddle_comments_none');
$ddle_limit = (int)get_option('ddle_limit');
$ddle_exclude = get_option('ddle_exclude');
$ddle_include = get_option('ddle_include');
$ddle_max_per_cat = get_option('ddle_max_per_cat');
$hide_check = '';
if ($ddle_hide_protected) {
$hide_check = " AND post_password = '' ";
}
$sort_code = 'ORDER BY cat_name ASC, post_date DESC';
switch ($ddle_sort) {
case 'cata':
$sort_code = 'GROUP BY cat_name, post_date DESC';
break;
case 'catd':
$sort_code = 'ORDER BY cat_name DESC, post_date DESC';
break;
case 'datea':
$sort_code = 'ORDER BY post_date ASC';
break;
case 'dated':
$sort_code = 'ORDER BY post_date DESC';
break;
}
$the_output = NULL;
$mysqlnow = current_time('mysql');
if ($ver < 2.3) {
$cat_list_code = '';
if (trim($ddle_exclude) != '') {
$ddle_exclude = (array)explode(',', $ddle_exclude);
foreach ($ddle_exclude as $de) {
$cat_list_code .= " AND cat_ID != '" . trim(mysql_escape_string($de)) . "' ";
}
} else if (trim($ddle_include) != '') {
$ddle_include = (array)explode(',', $ddle_include);
$cat_list_code .= " AND ( ";
$cat_tmp = array();
foreach ($ddle_include as $de) {
$cat_tmp[] .= " cat_ID = '" . trim(mysql_escape_string($de)) . "' ";
}
$cat_list_code .= implode(" OR ", $cat_tmp);
$cat_list_code .= " ) ";
}
$last_posts = (array)$wpdb->get_results("
SELECT post_date, ID, post_title, cat_name, cat_ID
FROM {$tp}posts, {$tp}post2cat, {$tp}categories
WHERE {$tp}posts.ID = {$tp}post2cat.post_id
AND {$tp}categories.cat_ID = {$tp}post2cat.category_id
AND post_status = 'publish'
AND post_type != 'page'
AND post_date < '$mysqlnow'
{$cat_list_code}
{$hide_check}
{$sort_code}
");
} else { // post 2.3
$cat_list_code = '';
if (trim($ddle_exclude) != '') {
$ddle_exclude = (array)explode(',', $ddle_exclude);
foreach ($ddle_exclude as $de) {
$cat_list_code .= " AND {$tp}terms.term_id != '" . trim(mysql_escape_string($de)) . "' ";
}
} else if (trim($ddle_include) != '') {
$ddle_include = (array)explode(',', $ddle_include);
$cat_list_code .= " AND ( ";
$cat_tmp = array();
foreach ($ddle_include as $de) {
$cat_tmp[] .= " {$tp}terms.term_id = '" . trim(mysql_escape_string($de)) . "' ";
}
$cat_list_code .= implode(" OR ", $cat_tmp);
$cat_list_code .= " ) ";
}
$last_posts = (array)$wpdb->get_results("
SELECT post_date,
ID,
post_title,
{$tp}terms.name as cat_name,
{$tp}terms.term_id as cat_ID
FROM {$tp}posts, {$tp}terms, {$tp}term_taxonomy, {$tp}term_relationships
WHERE {$tp}posts.ID = {$tp}term_relationships.object_id
AND {$tp}term_relationships.object_id = {$tp}posts.ID
AND {$tp}term_relationships.term_taxonomy_id = {$tp}term_taxonomy.term_taxonomy_id
AND {$tp}terms.term_id = {$tp}term_taxonomy.term_id
AND {$tp}term_taxonomy.taxonomy = 'category'
AND post_status = 'publish'
AND post_type = 'post'
AND post_date < '$mysqlnow'
{$cat_list_code}
{$hide_check}
{$sort_code}
");
}
if (empty($last_posts)) {
return NULL;
}
$the_output .= stripslashes($ddle_header);
if ($ddle_sort == 'datea') {
$last_posts = array_reverse($last_posts);
}
if ((int)$ddle_max_per_cat > 0) {
$used_cats = array();
foreach ($last_posts as $posts) {
$used_cats[$posts->cat_ID] = $ddle_max_per_cat;
}
$i = 0;
foreach ($last_posts as $posts) {
if ($used_cats[$posts->cat_ID] > 0) {
$used_cats[$posts->cat_ID] -= 1;
} else {
unset($last_posts[$i]);
}
$i++;
}
$last_posts = array_values($last_posts);
}
if ($ddle_sort == 'datea') {
$last_posts = array_reverse($last_posts);
}
$the_output .= '';
$limit_check = 0;
$last_cat = NULL;
$multi_cats = FALSE;
if ($ddle_max_per_cat > 1)
$multi_cats = TRUE;
$result = array();
foreach ($last_posts as $posts) {
if (($ddle_limit == 0) || ($limit_check < $ddle_limit)) {
if ($last_cat != $posts->cat_ID) {
if ($last_cat != NULL) {
$the_output .= ($multi_cats) ? '
' : '';
}
$the_output .= '';
$the_output .= '';
if ($ddle_cat_links) {
$the_output .= '' . $posts->cat_name . '';
} else {
$the_output .= $posts->cat_name;
}
$the_output .= ': ';
$the_output .= ($multi_cats) ? '- ' : '';
} else {
$the_output .= '
- ';
}
$the_output .= '' . $posts->post_title . '';
if ($ddle_show_date) {
$the_output .= ' - ' . date_i18n($ddle_date_format, strtotime($posts->post_date));
}
if ($ddle_comment_link) {
$result = "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = '1' AND comment_post_ID = '" . $posts->ID . "'";
$num_comments = $wpdb->get_var($result);
if ($ddle_comments_none || ($num_comments != 0)) {
$the_output .= ' - ';
if ($ddle_comment_num) {
$the_output .= $num_comments . ' Comment';
if ($num_comments != 1) {
$the_output .= 's';
}
} else {
$the_output .= 'Comments';
}
$the_output .= '';
}
}
if ($ddle_excerpts) {
$the_text = $wpdb->get_var("SELECT post_content FROM " . $tp . "posts WHERE ID=" . $posts->ID);
$the_output .= '
';
$the_output .= substr(strip_tags(trim($the_text)) , 0, $ddle_chars);
if (strlen(strip_tags(trim($the_text))) > $ddle_chars) {
$the_output .= "...";
}
}
$the_output .= ' ';
$last_cat = $posts->cat_ID;
} // end limit if
$limit_check++;
}
$the_output .= '
';
$the_output .= ($multi_cats) ? '' : '';
return $the_output;
}
function ddle_generate($content) {
if (strpos($content, "") !== FALSE) {
$content = str_replace("", dd_last_from_each(), $content);
}
return $content;
}
add_filter('the_content', 'ddle_generate');
add_action('admin_menu', 'ddle_add_option_pages');
?>