[wp-hackers] archives API

carlos campderrós gilipollas.desconcertante at gmail.com
Mon Jun 5 19:10:03 GMT 2006


Hi all,

I've been looking around for some plugin to get the archives in a cool way.
I've seen the super-archives-plugin [1] and some other, but all of these
failed my requirements: they all need javascript enabled to work. Then I
realized that archives API cannot provide data to make the archives looking
cool. I struggled with the wp-includes/template-functions-general.php and
more concisely with wp_get_archives & get_archives functions to get what I
needed. Below are the code of the modified functions, and the code of a page
(template) I made for showing up the archives (still need to be css'ed and
javascript'ed). You can see it working at
http://www.thinkingrid.com/blog/?page_id=7 . It's possible to add this (or
similar functionality) to the wordpress core?

Here it is a list of changes and the source code for the modified functions
below (I'm sorry I don't know how to make a patch using diff):

+ Added option 'yearly' to the $type parameter.
Do you need me to explain this? ;-)

+ Added option 'array' to the $format parameter.
A bidimensional array is returned.
$arr[0]: Numeric year, month or day, with leading zeros. This depends on
$type parameter. On weekly or postbypost it's the text of the link (I didn't
need anything in this case).
$arr[1] = Link to the archives with get_archives_link( ... );

+ Added parameters $before_day and $after_day.
To get posts published between $after_day and $before_day. They are strings
representing mysql DATETIME format.

+ Added the show_post_count option to the $type=daily.

I think these are all the changes I made. I think all changes I added are
embedded in <kiusap> and </kiusap> tags.

[1] Super-archives plugin:
http://www.jonas.rabbe.com/archives/2005/05/08/super-archives-plugin-for-wordpress/

Salute!



Here is the code for the two functions (more below the source for my
archives template):

------------------8<----------------------------------------

function wp_get_archives($args = '') {
    parse_str($args, $r);
    if ( !isset($r['type']) )
        $r['type'] = '';
    if ( !isset($r['limit']) )
        $r['limit'] = '';
    if ( !isset($r['format']) )
        $r['format'] = 'html';
    if ( !isset($r['before']) )
        $r['before'] = '';
    if ( !isset($r['after']) )
        $r['after'] = '';
    if ( !isset($r['show_post_count']) )
        $r['show_post_count'] = false;
//    <kiusap>
    if ( !isset($r['before_day']) )
        $r['before_day'] = '';
    if ( !isset($r['after_day']) )
        $r['after_day'] = '';
// </kiusap>
//    <kiusap>
    return
//    </kiusap>
    get_archives($r['type'], $r['limit'], $r['format'], $r['before'],
$r['after'], $r['show_post_count']
//    <kiusap>
, $r['after_day'], $r['before_day']
//    </kiusap>
);
}


function get_archives($type='', $limit='', $format='html', $before = '',
$after = '', $show_post_count = false
//    <kiusap>
, $after_day='', $before_day=''
//    </kiusap>
) {
    global $month, $wpdb;

    // <kiusap>
    if ( $format == 'array' )
    {
        $return_array = array();
    }
    // </kiusap>

    if ( '' == $type )
        $type = 'monthly';

    if ( '' != $limit ) {
        $limit = (int) $limit;
        $limit = ' LIMIT '.$limit;
    }
    // this is what will separate dates on weekly archive links
    $archive_week_separator = '&#8211;';

    // over-ride general date format ? 0 = no: use the date format set in
Options, 1 = yes: over-ride
    $archive_date_format_over_ride = 0;

    // options for daily archive (only if you over-ride the general date
format)
    $archive_day_date_format = 'Y/m/d';

    // options for weekly archive (only if you over-ride the general date
format)
    $archive_week_start_date_format = 'Y/m/d';
    $archive_week_end_date_format    = 'Y/m/d';

    if ( !$archive_date_format_over_ride ) {
        $archive_day_date_format = get_settings('date_format');
        $archive_week_start_date_format = get_settings('date_format');
        $archive_week_end_date_format = get_settings('date_format');
    }

    $add_hours = intval(get_settings('gmt_offset'));
    $add_minutes = intval(60 * (get_settings('gmt_offset') - $add_hours));

    $now = current_time('mysql');

    //    <kiusap>
    if ($before_day)
        $cond_filter_before = "post_date < '$before_day'";
    else
        $cond_filter_before = "post_date < '$now'";

    if ($after_day)
        $cond_filter_after = "post_date >= '$after_day'";
    else
        $cond_filter_after = "post_date != '0000-00-00 00:00:00'";
    //    </kiusap>
    //    <kiusap>
    if ( 'yearly' == $type ) {
        $arcresults = $wpdb->get_results("SELECT DISTINCT YEAR(post_date) AS
`year`, count(ID) as posts FROM $wpdb->posts WHERE $cond_filter_before AND
$cond_filter_after AND post_status = 'publish' GROUP BY YEAR(post_date)
ORDER BY post_date DESC" . $limit);
        if ( $arcresults ) {
            $ikiu = 0;
            $afterafter = $after;
            foreach ( $arcresults as $arcresult ) {
                $url    = get_year_link($arcresult->year);
                if ( $show_post_count ) {
                    $text = sprintf('%d', $arcresult->year);
                    $after = '&nbsp;('.$arcresult->posts.')' . $afterafter;
                } else {
                    $text = sprintf('%d', $arcresult->year);
                }
                if ($format == 'array')
                {
                    $return_array[$ikiu][0] = $text;
                    $return_array[$ikiu][1] = get_archives_link($url, $text,
$format, $before, $after);
                } else {
                    echo get_archives_link($url, $text, $format, $before,
$after);
                }
                $ikiu++;
            }
        }
    } else
    //    </kiusap>
    if ( 'monthly' == $type ) {
        //    <kiusap>
        //    $arcresults = $wpdb->get_results("SELECT DISTINCT
YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts
FROM $wpdb->posts WHERE post_date < '$now' AND post_date != '0000-00-00
00:00:00' AND post_status = 'publish' GROUP BY YEAR(post_date),
MONTH(post_date) ORDER BY post_date DESC" . $limit);
        $arcresults = $wpdb->get_results("SELECT DISTINCT YEAR(post_date) AS
`year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts
WHERE $cond_filter_after AND $cond_filter_before AND post_status = 'publish'
GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC" .
$limit);
        //    </kiusap>
        if ( $arcresults ) {
            //    <kiusap>
            $ikiu = 0;
            //    </kiusap>
            $afterafter = $after;
            foreach ( $arcresults as $arcresult ) {
                $url    = get_month_link($arcresult->year,
$arcresult->month);
                if ( $show_post_count ) {
                    $text = sprintf('%s %d',
$month[zeroise($arcresult->month,2)], $arcresult->year);
                    $after = '&nbsp;('.$arcresult->posts.')' . $afterafter;
                } else {
                    $text = sprintf('%s %d',
$month[zeroise($arcresult->month,2)], $arcresult->year);
                }
                // <kiusap>
                if ($format == 'array')
                {
                    $return_array[$ikiu][0] = zeroise($arcresult->month, 2);
                    $return_array[$ikiu][1] = get_archives_link($url, $text,
$format, $before, $after);
                } else {
                // </kiusap>
                    echo get_archives_link($url, $text, $format, $before,
$after);
                // <kiusap>
                }
                // </kiusap>
                //    <kiusap>
                $ikiu++;
                //    </kiusap>
            }
        }
    } elseif ( 'daily' == $type ) {
        //    <kiusap>
        //    $arcresults = $wpdb->get_results("SELECT DISTINCT
YEAR(post_date) AS `year`, MONTH(post_date) AS `month`,
DAYOFMONTH(post_date) AS `dayofmonth` FROM $wpdb->posts WHERE post_date <
'$now' AND post_date != '0000-00-00 00:00:00' AND post_status = 'publish'
ORDER BY post_date DESC" . $limit);
        $arcresults = $wpdb->get_results("SELECT DISTINCT YEAR(post_date) AS
`year`, MONTH(post_date) AS `month`, DAYOFMONTH(post_date) AS `dayofmonth`,
COUNT(ID) as posts FROM $wpdb->posts WHERE $cond_filter_after AND
$cond_filter_before AND post_status = 'publish' GROUP BY DAY(post_date)
ORDER BY post_date DESC" . $limit);
        //    </kiusap>
        if ( $arcresults ) {
            //    <kiusap>
            $ikiu = 0;
            //    </kiusap>
            //    <kiusap>
            $afterafter = $after;
            //    </kiusap>
            foreach ( $arcresults as $arcresult ) {
                $url    = get_day_link($arcresult->year, $arcresult->month,
$arcresult->dayofmonth);
                $date = sprintf("%d-%02d-%02d 00:00:00", $arcresult->year,
$arcresult->month, $arcresult->dayofmonth);
                $text = mysql2date($archive_day_date_format, $date);

                //    <kiusap>
                if ( $show_post_count ) {
                    $after = '&nbsp;('.$arcresult->posts.')' . $afterafter;
                }
                //    </kiusap>
                // <kiusap>
                if ($format == 'array')
                {
                    $return_array[$ikiu][0] =
zeroise($arcresult->dayofmonth, 2);
                    $return_array[$ikiu][1] = get_archives_link($url, $text,
$format, $before, $after);
                } else {
                // </kiusap>
                    echo get_archives_link($url, $text, $format, $before,
$after);
                // <kiusap>
                }
                // </kiusap>
                //    <kiusap>
                $ikiu++;
                //    </kiusap>
            }
        }
    } elseif ( 'weekly' == $type ) {
        $start_of_week = get_settings('start_of_week');
        //    <kiusap>
        //    $arcresults = $wpdb->get_results("SELECT DISTINCT
WEEK(post_date, $start_of_week) AS `week`, YEAR(post_date) AS yr,
DATE_FORMAT(post_date, '%Y-%m-%d') AS yyyymmdd FROM $wpdb->posts WHERE
post_date < '$now' AND post_status = 'publish' ORDER BY post_date DESC" .
$limit);
        $arcresults = $wpdb->get_results("SELECT DISTINCT WEEK(post_date,
$start_of_week) AS `week`, YEAR(post_date) AS yr, DATE_FORMAT(post_date,
'%Y-%m-%d') AS yyyymmdd FROM $wpdb->posts WHERE $cond_filter_after AND
$cond_filter_before AND post_status = 'publish' ORDER BY post_date DESC" .
$limit);
        //    </kiusap>
        $arc_w_last = '';
        if ( $arcresults ) {
            //    <kiusap>
            $ikiu = 0;
            //    </kiusap>
                foreach ( $arcresults as $arcresult ) {
                    if ( $arcresult->week != $arc_w_last ) {
                        $arc_year = $arcresult->yr;
                        $arc_w_last = $arcresult->week;
                        $arc_week = get_weekstartend($arcresult->yyyymmdd,
get_settings('start_of_week'));
                        $arc_week_start =
date_i18n($archive_week_start_date_format, $arc_week['start']);
                        $arc_week_end =
date_i18n($archive_week_end_date_format, $arc_week['end']);
                        $url  = sprintf('%s/%s%sm%s%s%sw%s%d',
get_settings('home'), '', '?', '=', $arc_year, '&amp;', '=',
$arcresult->week);
                        $text = $arc_week_start . $archive_week_separator .
$arc_week_end;
                        // <kiusap>
                        if ($format == 'array')
                        {
                            $return_array[$ikiu][0] = $text;
                            $return_array[$ikiu][1] =
get_archives_link($url, $text, $format, $before, $after);
                        } else {
                        // </kiusap>
                            echo get_archives_link($url, $text, $format,
$before, $after);
                        // <kiusap>
                        }
                        // </kiusap>
                    }
                //    <kiusap>
                $ikiu++;
                //    </kiusap>
                }
        }
    } elseif ( 'postbypost' == $type ) {
        $arcresults = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE
$cond_filter_before AND $cond_filter_after AND post_status = 'publish' ORDER
BY post_date DESC" . $limit);
        if ( $arcresults ) {
            //    <kiusap>
            $ikiu = 0;
            //    </kiusap>
            foreach ( $arcresults as $arcresult ) {
                if ( $arcresult->post_date != '0000-00-00 00:00:00' ) {
                    $url  = get_permalink($arcresult);
                    $arc_title = $arcresult->post_title;
                    if ( $arc_title )
                        $text = strip_tags($arc_title);
                    else
                        $text = $arcresult->ID;

                    // <kiusap>
                    if ($format == 'array')
                    {
                        $return_array[$ikiu][0] = $text;
                        $return_array[$ikiu][1] = get_archives_link($url,
$text, $format, $before, $after);
                    } else {
                    // </kiusap>
                        echo get_archives_link($url, $text, $format,
$before, $after);
                    // <kiusap>
                    }
                    // </kiusap>
                }
                //    <kiusap>
                $ikiu++;
                //    </kiusap>
            }
        }
    }
    // <kiusap>
    if ( $format == 'array' )
    {
        return $return_array;
    }
    // </kiusap>
}

---------->8---------------------------------------------

the archives template:

------------8<--------------------------------------------
<?php
/*
Template Name: Archives Looking Good
*/

/*
By Carlos Campderrós @ thinkingrid
campderros at thinkingrid dot com
gilipollas dot desconcertante at gmail dot com
*/
?>
<?php get_header(); ?>

<div id="content">
<h2>mis archivos bonitos</h2>
<?php
$anys = wp_get_archives('type=yearly&format=array&show_post_count=1');
if (count($anys))
{
    echo "<ul>\n";
}
foreach ($anys as $any)
{
    echo "<li>$any[1]\n";
    $after_day_any = "$any[0]-01-01+00:00:00";
    $before_day_any = ($any[0]+1)."-01-01+00:00:00";
    $mesos =
wp_get_archives("type=monthly&format=array&show_post_count=1&after_day=$after_day_any&before_day=$before_day_any");
    if (count($mesos))
    {
        echo "<ul>\n";
    }
    foreach ($mesos as $mes)
    {
        echo "<li>$mes[1]\n";

        $after_day_mes = "$any[0]-$mes[0]-01+00:00:00";
        $before_day_mes = ($any[0]+1)."-01-01+00:00:00";
        $before_day_mes = date("Y-m-d+00:00:00",
mktime(0,0,0,$mes[0]+1,1,$any[0]));

        $dies =
wp_get_archives("type=daily&format=array&show_post_count=1&after_day=$after_day_mes&before_day=$before_day_mes");

        if (count($dies))
        {
            echo "<ul>\n";
        }
        foreach ($dies as $dia)
        {
            echo "<li>$dia[1]\n";

            $after_day_dia = "$any[0]-$mes[0]-$dia[0]+00:00:00";
            $before_day_dia = date("Y-m-d+00:00:00",
mktime(0,0,0,$mes[0],$dia[0]+1,$any[0]));

            $posts =
wp_get_archives("type=postbypost&format=array&show_post_count=1&after_day=$after_day_dia&before_day=$before_day_dia");

            if (count($posts))
            {
                echo "<ul>\n";
            }
            foreach ($posts as $post)
            {
                echo "<li>$post[1]</li>\n";
            }
            if (count($posts))
            {
                echo "</ul>\n";
            }
            echo "</li>\n";
        }
        if (count($dies))
        {
            echo "</ul>\n";
        }
        echo "</li>\n";
    }
    if (count($mesos))
    {
        echo "</ul>\n";
    }
    echo "</li>\n";
}
if (count($anys))
{
    echo "</ul>\n";
}
?>

</div> <!-- content -->

<?php
get_sidebar();
get_footer();
?>
-------------------->8----------------------------------


-- 
Si no puedes deslumbrar con tu inteligencia,
desconcierta con tus gilipolleces


More information about the wp-hackers mailing list