Cómo ampliar la funcionalidad del calendario de Wordpress

El calendario que viene por defecto con WordPress está bastante bien, pero si queremos ampliar su funcionalidad para que sea compatible con “custom post types” y para poder alterar algunos de los elementos del calendario, es necesario incluir el siguiente código en el archivo functions.php de nuestro tema visual:

/* uccgetcalendar() :: Extends getcalendar() by including custom post types. * Derived from getcalendar() code in /wp-includes/general-template.php. / function ucc_get_calendar( $post_types = '' , $initial = true , $echo = true ) { global $wpdb, $m, $monthnum, $year, $wp_locale, $posts; if ( empty( $post_types ) || !is_array( $post_types ) ) { $args = array( 'public' => true , '_builtin' => false ); $output = 'names'; $operator = 'and'; $post_types = get_post_types( $args , $output , $operator ); $post_types = array_merge( $post_types , array( 'post' ) ); } else { / Trust but verify. / $my_post_types = array(); foreach ( $post_types as $post_type ) { if ( post_type_exists( $post_type ) ) $my_post_types[] = $post_type; } $post_types = $my_post_types; } $post_types_key = implode( '' , $post_types ); $post_types = "'" . implode( "' , '" , $post_types ) . "'"; $cache = array(); $key = md5( $m . $monthnum . $year . $post_types_key ); if ( $cache = wp_cache_get( 'get_calendar' , 'calendar' ) ) { if ( is_array( $cache ) && isset( $cache[$key] ) ) { remove_filter( 'get_calendar' , 'ucc_get_calendar_filter' ); $output = apply_filters( 'get_calendar', $cache[$key] ); add_filter( 'get_calendar' , 'ucc_get_calendar_filter' ); if ( $echo ) { echo $output; return; } else { return $output; } } } if ( !is_array( $cache ) ) $cache = array(); // Quick check. If we have no posts at all, abort! if ( !$posts ) { $sql = "SELECT 1 as test FROM $wpdb->posts WHERE post_type IN ( $post_types ) AND post_status = 'publish' LIMIT 1"; $gotsome = $wpdb->get_var( $sql ); if ( !$gotsome ) { $cache[$key] = ''; wp_cache_set( 'get_calendar' , $cache , 'calendar' ); return; } } if ( isset( $_GET['w'] ) ) $w = '' . intval( $_GET['w'] ); // week_begins = 0 stands for Sunday $week_begins = intval( get_option( 'start_of_week' ) ); // Let's figure out when we are if ( !empty( $monthnum ) && !empty( $year ) ) { $thismonth = '' . zeroise( intval( $monthnum ) , 2 ); $thisyear = ''.intval($year); } elseif ( !empty( $w ) ) { // We need to get the month from MySQL $thisyear = '' . intval( substr( $m , 0 , 4 ) ); $d = ( ( $w - 1 ) * 7 ) + 6; //it seems MySQL's weeks disagree with PHP's $thismonth = $wpdb->get_var( "SELECT DATE_FORMAT( ( DATE_ADD( '${thisyear}0101' , INTERVAL $d DAY ) ) , '%m' ) " ); } elseif ( !empty( $m ) ) { $thisyear = '' . intval( substr( $m , 0 , 4 ) ); if ( strlen( $m ) < 6 ) $thismonth = '01'; else $thismonth = '' . zeroise( intval( substr( $m , 4 , 2 ) ) , 2 ); } else { $thisyear = gmdate( 'Y' , current_time( 'timestamp' ) ); $thismonth = gmdate( 'm' , current_time( 'timestamp' ) ); } $unixmonth = mktime( 0 , 0 , 0 , $thismonth , 1 , $thisyear); // Get the next and previous month and year with at least one post $previous = $wpdb->get_row( "SELECT DISTINCT MONTH( post_date ) AS month , YEAR( post_date ) AS year FROM $wpdb->posts WHERE post_date < '$thisyear-$thismonth-01' AND post_type IN ( $post_types ) AND post_status = 'publish' ORDER BY post_date DESC LIMIT 1" ); $next = $wpdb->get_row( "SELECT DISTINCT MONTH( post_date ) AS month, YEAR( post_date ) AS year FROM $wpdb->posts WHERE post_date > '$thisyear-$thismonth-01' AND MONTH( post_date ) != MONTH( '$thisyear-$thismonth-01' ) AND post_type IN ( $post_types ) AND post_status = 'publish' ORDER BY post_date ASC LIMIT 1" ); / translators: Calendar caption: 1: month name, 2: 4-digit year */ $calendarcaption = _x( '%1$s %2$s' , 'calendar caption' ); $calendaroutput = '

‘; $myweek = array(); for ( $wdcount = 0 ; $wdcount getweekday( ( $wdcount + $weekbegins ) % 7 ); } foreach ( $myweek as $wd ) { $dayname = ( false $initial ) ? $wplocale->getweekdayinitial( $wd ) : $wplocale->getweekdayabbrev( $wd ); $wd = escattr( $wd ); $calendaroutput .= “ntt”; } $calendaroutput .= ‘
‘; if ( $previous ) { $calendaroutput .= “ntt” . ”; } else { $calendaroutput .= “ntt” . ‘
‘; } $calendaroutput .= “ntt” . ‘
‘; if ( $next ) { $calendar
output .= “ntt” . ‘
[‘ . sprintf( $calendarcaption , $wplocale->getmonth( $thismonth ) , date( ‘Y’ , $unixmonth ) ) . ‘$dayname[]( "' . sprintf( _( 'View posts for %1$s %2$s' ) , $wplocale->getmonth( $previous->month ) , date( ‘Y’ , mktime( 0 , 0 , 0 , $previous->month , 1 , $previous->year ) ) ) . ‘” href=”/news/archives/’.$previous->year.’/’.$previous->month.'”>« ‘ . $wplocale->getmonthabbrev( $wplocale->getmonth( $previous->month ) ) . ‘attr( sprintf( _( 'View posts for %1$s %2$s' ) , $wplocale->getmonth( $next->month ) , date( ‘Y’ , mktime( 0 , 0 , 0 , $next->month , 1 , $next->year ) ) ) ) . ‘” href=”/news/archives/’.$next->year.’/’.$next->month.'”>’ . $wplocale->getmonthabbrev( $wplocale->getmonth( $next->month ) ) . ‘ »‘; } else { $calendaroutput .= “ntt” . ‘
‘; } $calendaroutput .= ‘
‘; // Get days with posts $dayswithposts = $wpdb->get
results( “SELECT DISTINCT DAYOFMONTH( postdate ) FROM $wpdb->posts WHERE MONTH( postdate ) = ‘$thismonth’ AND YEAR( postdate ) = ‘$thisyear’ AND posttype IN ( $posttypes ) AND poststatus = ‘publish’ AND postdate < ‘” . currenttime( ‘mysql’ ) . ”’, ARRAYN ); if ( $dayswithposts ) { foreach ( (array) $dayswithposts as $daywith ) { $daywithpost[] = $daywith[0]; } } else { $daywithpost = array(); } if ( strpos( $SERVER[‘HTTPUSERAGENT’] , ‘MSIE’ ) ! false || stripos( $SERVER[‘HTTPUSERAGENT’] , ‘camino’ ) ! false || stripos( $SERVER[‘HTTPUSERAGENT’] , ‘safari’ ) ! false ) $aktitleseparator = “n”; else $aktitleseparator = ‘, ‘; $aktitlesforday = array(); $akposttitles = $wpdb->getresults( “SELECT ID, posttitle, DAYOFMONTH( postdate ) as dom ” . “FROM $wpdb->posts ” . “WHERE YEAR( postdate ) = ‘$thisyear’ ” . “AND MONTH( postdate ) = ‘$thismonth’ ” . “AND postdate < ‘” . currenttime( ‘mysql’ ) . “‘ ” . “AND posttype IN ( $posttypes ) AND poststatus = ‘publish'” ); if ( $akposttitles ) { foreach ( (array) $akposttitles as $akposttitle ) { $posttitle = escattr( applyfilters( ‘thetitle’ , $akposttitle->posttitle , $akposttitle->ID ) ); if ( empty( $aktitlesforday[‘day’ . $akposttitle->dom] ) ) $aktitlesforday[‘day’.$akposttitle->dom] = ”; if ( empty( $aktitlesforday[“$akposttitle->dom”] ) ) // first one $aktitlesforday[“$akposttitle->dom”] = $posttitle; else $aktitlesforday[“$akposttitle->dom”] .= $aktitleseparator . $posttitle; } } // See how much we should pad in the beginning $pad = calendarweekmod( date( ‘w’ , $unixmonth ) – $weekbegins ); if ( 0 != $pad ) $calendaroutput .= “ntt” . ”; $daysinmonth = intval( date( ‘t’ , $unixmonth ) ); for ( $day = 1 ; $day$day”; else $calendaroutput .= $day; $calendaroutput .= ”; if ( 6 == calendarweekmod( date( ‘w’ , mktime( 0 , 0 , 0 , $thismonth , $day , $thisyear ) ) – $weekbegins ) ) $newrow = true; } $pad = 7 – calendarweekmod( date( ‘w’ , mktime( 0 , 0 , 0 , $thismonth , $day , $thisyear ) ) – $weekbegins ); if ( $pad != 0 && $pad != 7 ) $calendaroutput .= “ntt” . ‘
‘; $calendar_output .= “ntntnt

Joan Mira

Interactive web developer and creative technologist in London