Date Cloud

Como preparação para um novo layout aqui fica uma peça que vou precisar que é uma date cloud ou seja seguir o mesmo principio para criar uma tag cloud mas usar isso para criar os links para o arquivo.
O ideal é depois transformar isto num plugin mas por enquanto limitei-me a ver qual o comportamento normal do wordpress e fiz as alterações necessárias para obter aquilo que pretendia.
em baixo está o exemplo concreto bem como o código.
algumas notas:

  • alterar a função get_archives_link que está em general-template.php
    tiver que acrescentar mais um tipo de formato (habitualmente link ou html) porque não pude usar o custom já que ele inseria automaticamente uma quebra de linha
  • alterar a função get_archives_link de forma a fazer o processamento necessário para a tag cloud. tão simples quanto ver qual o mês com mais posts e depois enquadrar cada mês no text-size correspondente: smallest, small, normal, large, largest
‘monthly’, ‘limit’ => ”,
‘format’ => ‘html’, ‘before’ => ”,
‘after’ => ”, ‘show_post_count’ => false
);

$r = wp_parse_args( $args, $defaults );
extract( $r, EXTR_SKIP );

if ( ” == $type )
$type = ‘monthly’;

if ( ” != $limit ) {
$limit = absint($limit);
$limit = ‘ LIMIT ‘.$limit;
}

$before = ‘‘;
$after = ‘
 ‘;

// this is what will separate dates on weekly archive links
$archive_week_separator = ‘–’;

// 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_option(‘date_format’);
$archive_week_start_date_format = get_option(‘date_format’);
$archive_week_end_date_format = get_option(‘date_format’);
}

//filters
$where = apply_filters(‘getarchives_where’, “WHERE post_type = ‘post’ AND post_status = ‘publish'”, $r );
$join = apply_filters(‘getarchives_join’, “”, $r);

if ( ‘monthly’ == $type ) {
$query = “SELECT DISTINCT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC $limit”;
$key = md5($query);
$cache = wp_cache_get( ‘wp_get_archives’ , ‘general’);
if ( !isset( $cache[ $key ] ) ) {
$arcresults = $wpdb->get_results($query);
$cache[ $key ] = $arcresults;
wp_cache_add( ‘wp_get_archives’, $cache, ‘general’ );
} else {
$arcresults = $cache[ $key ];
}
if ( $arcresults ) {
$max=0;
foreach ( $arcresults as $arcresult ) {
if ($arcresult->posts > $max) $max = $arcresult->posts;
}
}

if ( $arcresults ) {
$afterafter = $after;
foreach ( $arcresults as $arcresult ) {
$url = get_month_link($arcresult->year, $arcresult->month);
$text = sprintf(__(‘%1$s %2$d’), substr($wp_locale->get_month($arcresult->month), 0, 3), $arcresult->year);

$tamanho = ”;
if (($arcresult->posts / $max) >= (6/7))
$tamanho = ‘xx-large’;
elseif (($arcresult->posts / $max) >= (5/7))
$tamanho = ‘x-large’;
elseif (($arcresult->posts / $max) >= (4/7))
$tamanho = ‘large’;
elseif (($arcresult->posts / $max) >= (3/7))
$tamanho = ‘medium’;
elseif (($arcresult->posts / $max) >= (2/7))
$tamanho = ‘small’;
elseif (($arcresult->posts / $max) >= (1/7))
$tamanho = ‘x-small’;
elseif (($arcresult->posts / $max) >= (0))
$tamanho = ‘xx-small’;

if ( $show_post_count )
$after = ‘ (‘.$arcresult->posts.’)’ . $afterafter;
echo vs_get_archives_link($url, $text, ‘dataCloud’, sprintf($before, $tamanho), $after);
}
}
}

/* link navigation hack by Orien http://icecode.com/ */
function vs_get_archives_link($url, $text, $format = ‘html’, $before = ”, $after = ”) {
$text = wptexturize($text);
$title_text = attribute_escape($text);
$url = clean_url($url);

if (‘link’ == $format)
$link_html = “\t\n”;
elseif (‘option’ == $format)
$link_html = “\t\n”;
elseif (‘html’ == $format)
$link_html = “\t

  • $before$text$after
  • \n”;
    elseif (‘dataCloud’ == $format)
    $link_html = “$before$text$after”;
    else // custom
    $link_html = “\t$before$text$after\n”;

    $link_html = apply_filters( “get_archives_link”, $link_html );

    return $link_html;
    }

    ?>

    $args = 'type=monthly&format=link&before=I&after=A&show_post_count=1';
    global $wpdb, $wp_locale;
    
    $defaults = array(
    	'type' => 'monthly', 'limit' => '',
    	'format' => 'html', 'before' => '',
    	'after' => '', 'show_post_count' => false
    );
    
    $r = wp_parse_args( $args, $defaults );
    extract( $r, EXTR_SKIP );
    
    if ( '' == $type )
    	$type = 'monthly';
    
    if ( '' != $limit ) {
    	$limit = absint($limit);
    	$limit = ' LIMIT '.$limit;
    }
    
    $before = '';
    $after = ' ';
    
    // this is what will separate dates on weekly archive links
    $archive_week_separator = '–';
    
    // 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_option('date_format');
    	$archive_week_start_date_format = get_option('date_format');
    	$archive_week_end_date_format = get_option('date_format');
    }
    
    //filters
    $where = apply_filters('getarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'", $r );
    $join = apply_filters('getarchives_join', "", $r);
    
    if ( 'monthly' == $type ) {
    	$query = "SELECT DISTINCT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC $limit";
    	$key = md5($query);
    	$cache = wp_cache_get( 'wp_get_archives' , 'general');
    	if ( !isset( $cache[ $key ] ) ) {
    		$arcresults = $wpdb->get_results($query);
    		$cache[ $key ] = $arcresults;
    		wp_cache_add( 'wp_get_archives', $cache, 'general' );
    	} else {
    		$arcresults = $cache[ $key ];
    	}
    	if ( $arcresults ) {
    		$max=0;
    		foreach ( $arcresults as $arcresult ) {
    			if ($arcresult->posts > $max) $max = $arcresult->posts;
    		}
    	}
    
    	if ( $arcresults ) {
    		$afterafter = $after;
    		foreach ( $arcresults as $arcresult ) {
    			$url	= get_month_link($arcresult->year,	$arcresult->month);
    			$text = sprintf(__('%1$s %2$d'), substr($wp_locale->get_month($arcresult->month), 0, 3), $arcresult->year);
    
    			$tamanho = '';
    			if (($arcresult->posts / $max) >= (4/5))
    				$tamanho = 'largest';
    			elseif (($arcresult->posts / $max) >= (3/5))
    				$tamanho = 'large';
    			elseif (($arcresult->posts / $max) >= (2/5))
    				$tamanho = 'normal';
    			elseif (($arcresult->posts / $max) >= (1/5))
    				$tamanho = 'small';
    			else
    				$tamanho = 'smaller';
    
    			if ( $show_post_count )
    				$after = ' ('.$arcresult->posts.')' . $afterafter;
    			echo vs_get_archives_link($url, $text, 'dataCloud', sprintf($before, $tamanho), $after);
    		}
    	}
    }
    
    
    /* link navigation hack by Orien http://icecode.com/ */
    function vs_get_archives_link($url, $text, $format = 'html', $before = '', $after = '') {
    	$text = wptexturize($text);
    	$title_text = attribute_escape($text);
    	$url = clean_url($url);
    
    	if ('link' == $format)
    		$link_html = "\t\n";
    	elseif ('option' == $format)
    		$link_html = "\t\n";
    	elseif ('html' == $format)
    		$link_html = "\t
  • $before$text$after
  • \n"; elseif ('dataCloud' == $format) $link_html = "$before$text$after"; else // custom $link_html = "\t$before$text$after\n"; $link_html = apply_filters( "get_archives_link", $link_html ); return $link_html; }

    Deixe um comentário

    O seu endereço de email não será publicado. Campos obrigatórios marcados com *