Latest Post From Each Category');
add_option('mlp_sort', 'cata');
add_option('mlp_cat_links', TRUE);
add_option('mlp_hide_protected', TRUE);
add_option('mlp_show_date', TRUE);
add_option('mlp_date_format', 'F jS, Y');
add_option('mlp_excerpts', FALSE);
add_option('mlp_chars', 200);
add_option('mlp_comment_link', FALSE);
add_option('mlp_comment_num', FALSE);
add_option('mlp_comments_none', FALSE);
add_option('mlp_limit', 0);
function mlp_add_option_pages() {
if (function_exists('add_options_page')) {
add_options_page('Latest Posts', 'Latest Posts', 8, __FILE__, 'mlp_options_page');
}
}
function mlp_options_page() {
global $mlp_version;
if (isset($_POST['set_defaults'])) {
echo '
';
update_option('mlp_header', 'Latest Post From Each Category
');
update_option('mlp_sort', 'cata');
update_option('mlp_cat_links', TRUE);
update_option('mlp_hide_protected', TRUE);
update_option('mlp_show_date', TRUE);
update_option('mlp_date_format', 'F jS, Y');
update_option('mlp_excerpts', FALSE);
update_option('mlp_chars', 200);
update_option('mlp_comment_link', FALSE);
update_option('mlp_comment_num', FALSE);
update_option('mlp_comments_none', FALSE);
update_option('mlp_limit', 0);
echo 'Default Options Loaded!';
echo '
';
} else if (isset($_POST['info_update'])) {
update_option('mlp_header', (string) $_POST["mlp_header"]);
update_option('mlp_sort', (string) $_POST["mlp_sort"]);
update_option('mlp_cat_links', (string) $_POST["mlp_cat_links"]);
update_option('mlp_hide_protected', (bool)$_POST["mlp_hide_protected"]);
update_option('mlp_show_date', (bool) $_POST["mlp_show_date"]);
update_option('mlp_date_format', (string) $_POST["mlp_date_format"]);
update_option('mlp_excerpts', (bool) $_POST["mlp_excerpts"]);
update_option('mlp_chars', (string) $_POST["mlp_chars"]);
update_option('mlp_comment_link', (bool) $_POST["mlp_comment_link"]);
update_option('mlp_comment_num', (bool) $_POST["mlp_comment_num"]);
update_option('mlp_comments_none', (bool) $_POST["mlp_comments_none"]);
update_option('mlp_limit', (string) $_POST["mlp_limit"]);
echo 'Configuration Updated!';
echo '';
} ?>
Latest Posts From Each Category v
prefix;
$mlp_header = get_option('mlp_header');
$mlp_sort = get_option('mlp_sort');
$mlp_cat_links = get_option('mlp_cat_links');
$mlp_hide_protected = get_option('mlp_hide_protected');
$mlp_show_date = get_option('mlp_show_date');
$mlp_date_format = get_option('mlp_date_format');
$mlp_excerpts = get_option('mlp_excerpts');
$mlp_chars = (int)get_option('mlp_chars');
$mlp_comment_link = (bool)get_option('mlp_comment_link');
$mlp_comment_num = (bool)get_option('mlp_comment_num');
$mlp_comments_none = (bool)get_option('mlp_comments_none');
$mlp_limit = (int)get_option('mlp_limit');
$hide_check = '';
if ($mlp_hide_protected) {
$hide_check = " AND post_password = '' ";
}
$sort_code = 'ORDER BY cat_name ASC, post_date DESC';
switch ($mlp_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;
if ($ver < 2.3) {
throw "don't support pre-2.3 wordpress";
} else { // post 2.3
$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 < NOW()
{$hide_check}
{$sort_code}
");
}
if (empty($last_posts)) {
return NULL;
}
$the_output .= stripslashes($mlp_header);
if ($mlp_sort == 'datea') {
$last_posts = array_reverse($last_posts);
}
$used_cats = array();;
$i = 0;
foreach ($last_posts as $posts) {
if (false)
{
if (in_array($posts->cat_name, $used_cats)) {
unset($last_posts[$i]);
} else {
$used_cats[] = $posts->cat_name;
}
}
$i++;
}
$last_posts = array_values($last_posts);
if ($mlp_sort == 'datea') {
$last_posts = array_reverse($last_posts);
}
$the_output .= '';
$limit_check = 0;
foreach ($last_posts as $posts) {
if (($mlp_limit == 0) || ($limit_check < $mlp_limit)) {
$the_output .= '- ';
if ($mlp_cat_links) {
$the_output .= '' . $posts->cat_name . '';
} else {
$the_output .= $posts->cat_name;
}
$the_output .= ': ' . $posts->post_title . '';
if ($mlp_show_date) {
$the_output .= ' - ' . date_i18n($mlp_date_format, strtotime($posts->post_date));
}
if ($mlp_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 ($mlp_comments_none || ($num_comments != 0)) {
$the_output .= ' - ';
if ($mlp_comment_num) {
$the_output .= $num_comments . ' Comment';
if ($num_comments != 1) {
$the_output .= 's';
}
} else {
$the_output .= 'Comments';
}
$the_output .= '';
}
}
if ($mlp_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, $mlp_chars);
if (strlen(strip_tags(trim($the_text))) > $mlp_chars) {
$the_output .= "...";
}
}
$the_output .= ' ';
} // end limit if
$limit_check++;
}
$the_output .= '
';
return $the_output;
}
function mlp_generate($content) {
if (strpos($content, "") !== FALSE) {
$content = str_replace("", mlp_last_from_each(), $content);
}
return $content;
}
add_filter('the_content', 'mlp_generate');
add_action('admin_menu', 'mlp_add_option_pages');
?>