If you have seen Queries like those:
SELECT * FROM wp_posts p LEFT OUTER JOIN wp_term_relationships r ON r.object_id = p.ID LEFT OUTER JOIN wp_term_taxonomy x ON x.term_taxonomy_id = r.term_taxonomy_id LEFT OUTER JOIN wp_terms t ON t.term_id = x.term_id WHERE p.post_status = 'publish' AND p.post_type = 'post' AND t.slug = 'news-archiv'
then you know what i am talking about.
IT’S DAMN HARD!
post-tags-and-archives.php
from http://www.oxpal.com/main.php?o=dev_wordpress_pta
THE INTERESTING PART:
News-Archiv
is the name of the category. We do not want to get any other posts than those in that category.
function wp_get_archives( $args = '' )
does not offer such functionality.
[cc lang=”php” escaped=”true” width=”600″]
$categoryID = get_cat_ID(“News-Archiv”);
$args = array(
‘category’ => $categoryID
);
$posts = get_posts( $args ); // does not return posts sorted by month or year.
$groupData = array();
foreach ( $posts as $post )
{
$date = date_parse($post->post_date);
$groupData[$date[‘year’]][$date[‘month’]][] = $post;
}
[/cc]
THE FULL CODE:
download as tar.gz: post-tags-and-archive.tar
[cc lang=”php” escaped=”true” width=”600″]
*/
$optionsArray [‘smallest’] = ’10’;
$optionsArray [‘largest’] = ’29’;
$optionsArray [‘number’] = null;
$optionsArray [‘orderby’] = ‘name’;
$optionsArray [‘order’] = ‘ASC’;
$optionsArray [‘exclude’] = null;
$optionsArray [‘include’] = null;
$optionsArray [‘format’] = ‘flat’;
$optionsArray [‘separator’] = “•“;
/* WOULD BE GREAT, IF ONE COULD PICK A CATEGORY IN BACKEND – UNTESTED!
$categories_array = get_categories();
$categories_string = ‘
// serialize
foreach ($categories_array as $key => $value)
{
$categories_string .= ‘Volvo’;
}
$categories_string .= ”;
$optionsArray [‘category’] = $categories_string;
*/
/*
* Reference list of arguments for archive: $args = array(
*/
$optionsArray [‘archives_type’] = ‘monthly’;
$optionsArray [‘archives_limit’] = null;
$optionsArray [‘archives_format’] = ‘html’;
$optionsArray [‘archives_before’] = null;
$optionsArray [‘archives_after’] = null;
$optionsArray [‘archives_show_post_count’] = false;
$optionsArray [‘archives_order’] = ‘DESC’;
return $optionsArray;
}
// add the admin page
function pta_posttagsandarchives_add_pages() {
add_options_page ( ‘Post Tags and Archives’, ‘Post Tags & Archives’, ‘manage_options’, __FILE__, ‘pta_posttagsandarchives_options’ );
}
function pta_posttagsandarchives_uninstall() {
delete_option ( ‘pta-posttagsandarchives_options’ );
// delete_option(‘pta-posttagsandarchives_widget’);
if (function_exists ( ‘add_shortcode’ )) {
remove_shortcode ( ‘POSTARCHIVES’ );
remove_shortcode ( ‘postarchives’ );
remove_shortcode ( ‘postarchive’ );
remove_shortcode ( ‘POSTARCHIVE’ );
remove_shortcode ( ‘posttags’ );
remove_shortcode ( ‘POSTTAGS’ );
}
return;
} // uninstall
// shortcode function
function pta_posttags_shortcode($atts = NULL) {
return pta_GetPostTags ();
}
function pta_postarchives_shortcode($atts = NULL) {
return pta_GetPostArchives ();
}
// php function for integration
function pta_posttags($atts = NULL) {
echo pta_GetPostTags ();
}
function pta_postarchives($atts = NULL) {
echo pta_GetPostArchives ();
}
// replace tag in content with tag cloud (non-shortcode version for WP 2.3.x)
function pta_posttagsandarchives_init($content) {
$postTags = true;
$postArchives = true;
if (strpos ( $content, ‘[POSTTAGS]’ ) === false) {
$postTags = false;
}
;
if (strpos ( $content, ‘[POSTARCHIVES]’ ) === false) {
$postArchives = false;
}
;
if ($postTags) {
$code = pta_GetPostTags ();
$content = str_replace ( ‘[POSTTAGS]’, $code, $content );
}
if ($postArchives) {
$code = pta_GetPostArchive ();
$content = str_replace ( ‘[POSTARCHIVES]’, $code, $content );
}
return $content;
} // init
function pta_GetPostTags() {
$args = pta_posttagsandarchives_getargs ();
$encloseBegin = ‘
‘;
// echo $encloseBegin;
$code = $encloseBegin . wp_tag_cloud ( $args ) . $encloseEnd;
return $code;
}
function pta_GetPostArchives() {
$args = pta_posttagsandarchives_getargs_archives();
$tEncloseBegin = “”;
$tEncloseEnd = “”;
if ($args [‘format’] == ‘html’) {
$tEncloseBegin = ”
-
- “;
- $tEncloseEnd = “
“;
}
$encloseBegin = ‘
$encloseEnd = $tEncloseEnd . ‘
‘;
// echo $encloseBegin;
$categoryID = get_cat_ID(“News-Archiv”);
$args = array(
‘category’ => $categoryID
);
$posts = get_posts( $args ); // does not return posts sorted by month or year.
$groupData = array();
foreach ( $posts as $post )
{
$date = date_parse($post->post_date);
$groupData[$date[‘year’]][$date[‘month’]][] = $post;
}
$lang = get_bloginfo(‘language’); // get lang of current post (i hope so)
if($lang == ‘de-DE’)
{
$month_names = array(
1=>”Januar”,
2=>”Februar”,
3=>”März”,
4=>”April”,
5=>”Mai”,
6=>”Juni”,
7=>”Juli”,
8=>”August”,
9=>”September”,
10=>”Oktober”,
11=>”November”,
12=>”Dezember”);
}
else // assume english
{
$month_names = array(”, ‘January’, ‘February’, ‘March’, ‘April’, ‘May’, ‘June’, ‘July’, ‘August’, ‘September’, ‘October’, ‘November’, ‘December’);
}
foreach ( $groupData as $year => $month_array )
{
foreach ( $month_array as $month_number => $posts )
{
$month_string = $month_names[$month_number]; // 3 -> March/März
$siteurl = get_site_url();;
$html_output .= ‘
‘;
/* currently no need to iterate over all posts
foreach ( $posts as $key => $post )
{
print_r($post);
}
*/
}
}
$code = $encloseBegin . $html_output . $encloseEnd;
// $code = $encloseBegin.wp_get_archives($args).$encloseEnd; // convenient, but does not allow to filter out a category ‘News-Archiv’
return $code;
}
function pta_posttagsandarchives_getargs() {
$tempoptions = get_option ( ‘pta-posttagsandarchives_options’ );
$args = array (
‘smallest’ => $tempoptions [‘smallest’],
‘largest’ => $tempoptions [‘largest’],
‘number’ => $tempoptions [‘number’],
‘orderby’ => $tempoptions [‘orderby’],
‘order’ => $tempoptions [‘order’],
‘exclude’ => $tempoptions [‘exclude’],
‘include’ => $tempoptions [‘include’],
‘format’ => $tempoptions [‘format’],
‘separator’ => “\n” . $tempoptions [‘separator’],
‘taxonomy’ => ‘post_tag’,
‘link’ => ‘view’,
‘echo’ => false
)
;
return $args;
}
function pta_posttagsandarchives_getargs_archives() {
$tempoptions = get_option ( ‘pta-posttagsandarchives_options’ );
$args = array (
‘type’ => $tempoptions [‘archives_type’],
‘limit’ => $tempoptions [‘archives_limit’],
‘format’ => $tempoptions [‘archives_format’],
‘before’ => $tempoptions [‘archives_before’],
‘after’ => $tempoptions [‘archives_after’],
‘show_post_count’ => $tempoptions [‘archives_show_post_count’],
‘echo’ => false,
// ‘echo’ => $tempoptions[‘archives_echo’],
‘order’ => $tempoptions [‘archives_order’]
);
return $args;
}
// options page
function pta_posttagsandarchives_options() {
$options = $newoptions = get_option ( ‘pta-posttagsandarchives_options’ );
// if submitted, process results
if ($_POST [“pta-posttagsandarchives_submit”]) {
$arrayDefaults = array ();
$arrayDefaults = pta_getDefaults ( $arrayDefaults );
$tVal = null;
// Tags
$tVal = strip_tags ( stripslashes ( $_POST [“smallest”] ) );
$newoptions [‘smallest’] = is_numeric ( $tVal ) ? $tVal : $arrayDefaults [“smallest”];
$tVal = strip_tags ( stripslashes ( $_POST [“largest”] ) );
$newoptions [‘largest’] = is_numeric ( $tVal ) ? $tVal : $arrayDefaults [“largest”];
$tVal = strip_tags ( stripslashes ( $_POST [“number”] ) );
$newoptions [‘number’] = is_numeric ( $tVal ) ? $tVal : $arrayDefaults [“number”];
$newoptions [‘orderby’] = strip_tags ( stripslashes ( $_POST [“orderby”] ) );
$newoptions [‘order’] = strip_tags ( stripslashes ( $_POST [“order”] ) );
// $newoptions[‘exclude’] = strip_tags(stripslashes($_POST[“exclude”]));
// $newoptions[‘include’] = strip_tags(stripslashes($_POST[“include”]));
$tstring = html_entity_decode ( stripslashes ( $_POST [“separator”] ) );
$tstring = str_replace ( “\\n”, “\n”, $tstring );
$newoptions [‘separator’] = $tstring;
// archive
$newoptions [‘archives_type’] = strip_tags ( stripslashes ( $_POST [“archives_type”] ) );
$tVal = strip_tags ( stripslashes ( $_POST [“archives_limit”] ) );
if (! is_numeric ( $tVal ))
$tVal = $arrayDefaults [“archives_limit”];
if ($tVal == 0)
$tVal = null;
$newoptions [‘archives_limit’] = $tVal;
$newoptions [‘archives_format’] = strip_tags ( stripslashes ( $_POST [“archives_format”] ) );
$tstring = html_entity_decode ( stripslashes ( $_POST [“archives_before”] ) );
$tstring = str_replace ( “\\n”, “\n”, $tstring );
$newoptions [‘archives_before’] = $tstring;
$tstring = html_entity_decode ( stripslashes ( $_POST [“archives_after”] ) );
$tstring = str_replace ( “\\n”, “\n”, $tstring );
$newoptions [‘archives_after’] = $tstring;
$newoptions [‘archives_show_post_count’] = $_POST [“archives_show_post_count”];
$newoptions [‘archives_order’] = $_POST [“archives_order”];
echo ‘
‘ . __ ( ‘Options changed.’, ‘pta-posttagsandarchives’ ) . ‘
‘;
}
// any changes? save!
if ($options != $newoptions) {
$options = $newoptions;
update_option ( ‘pta-posttagsandarchives_options’, $options );
}
// options form
echo ‘
‘;
}
// add the actions
add_action ( ‘admin_menu’, ‘pta_posttagsandarchives_add_pages’ );
register_activation_hook ( __FILE__, ‘pta_posttagsandarchives_install’ );
register_deactivation_hook ( __FILE__, ‘pta_posttagsandarchives_uninstall’ );
if (function_exists ( ‘add_shortcode’ )) {
add_shortcode ( ‘POSTARCHIVES’, ‘pta_postarchives_shortcode’ );
add_shortcode ( ‘postarchives’, ‘pta_postarchives_shortcode’ );
add_shortcode ( ‘postarchive’, ‘pta_postarchives_shortcode’ );
add_shortcode ( ‘POSTARCHIVE’, ‘pta_postarchives_shortcode’ );
add_shortcode ( ‘posttags’, ‘pta_posttags_shortcode’ );
add_shortcode ( ‘POSTTAGS’, ‘pta_posttags_shortcode’ );
} else {
add_filter ( ‘the_content’, ‘pta_posttagsandarchives_init’ );
}
// WordPress version check
// maybe one day
/*
* if (version_compare($wp_version, ‘2.5.0’, ‘<‘)) add_action(‘admin_notices’, wpp_update_warning’)); function wpp_update_warning() { $msg = ‘
‘.__(‘Your WordPress version is too old. WP Post Tags And Archives requires at least version 2.5 to function correctly. Please update your blog via Tools > Upgrade.’, ‘pta-posttagsandarchives’).’
‘; echo trim($msg); }
*/
?>
[/cc]
liked this article?
- only together we can create a truly free world
- plz support dwaves to keep it up & running!
- (yes the info on the internet is (mostly) free but beer is still not free (still have to work on that))
- really really hate advertisement
- contribute: whenever a solution was found, blog about it for others to find!
- talk about, recommend & link to this blog and articles
- thanks to all who contribute!