Añadir cambio de año a Widget Calendar de WordPress

  • Autor Autor calduchoweb
  • Fecha de inicio Fecha de inicio
C

calduchoweb

Curioso
¡Usuario con pocos negocios! ¡Utiliza siempre saldo de Forobeta!
Hola, necesito a ver si alguien me puede indicar cómo, dónde y qué código poner para modificar el widget Calendar de WordPress.

Este widget te permite cambiar el mes (anterior y siguiente), pero no da la opción de cambiar de año. Por más que estoy probando a modificar el archivo "general-template.php" (creo que es el que hay que modificar), no soy capaz de conseguirlo.

Si alguien me puede ayudar lo agradecería enormemente.

Muchas gracias!
 
Hola de nuevo. Tras muchas pruebas e intentos he conseguido hacer el link para el año anterior, pero no soy capaz de crear el del año siguiente. A ver si alguien me puede ayudar... A continuación pongo el código que tengo:

$previous = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
FROM $wpdb->posts
WHERE post_date < '$thisyear-$thismonth-01'
AND post_type = 'post' AND post_status = 'publish'
ORDER BY post_date DESC
LIMIT 1");
$previousyear = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
FROM $wpdb->posts
WHERE post_date < '$thisyear-01'
AND post_type = 'post' AND post_status = 'publish'
ORDER BY post_date DESC
LIMIT 1");
$previousmonth = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
FROM $wpdb->posts
WHERE post_date < 'm'
AND post_type = 'post' AND post_status = 'publish'
ORDER BY post_date DESC
LIMIT 1");
$next = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
FROM $wpdb->posts
WHERE post_date > '$thisyear-$thismonth-{$last_day} 23:59:59'
AND post_type = 'post' AND post_status = 'publish'
ORDER BY post_date ASC
LIMIT 1");
$nextyear = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
FROM $wpdb->posts
WHERE post_date > '$thisyear-01'
AND post_type = 'post' AND post_status = 'publish'
ORDER BY post_date ASC
LIMIT 1");
/* translators: Calendar caption: 1: month name, 2: 4-digit year */
$calendar_caption = _x('%1$s %2$s', 'calendar caption');
$calendar_output = '<table id="wp-calendar">
<caption>' . sprintf(
$calendar_caption,
$wp_locale->get_month( $thismonth ),
date( 'Y', $unixmonth )
) . '</caption>
<thead>
<tr>
<td>';

if ( $previousyear ) {
$calendar_output .= "\n\t\t".'<td colspan="3" id="prev"><a href="' . get_month_link($previousyear->year, $thismonth) . '" title="' . esc_attr( sprintf(__('View posts for %1$s %2$s'), $wp_locale->get_month($previousyear->year), date('Y', mktime(0, 0 , 0, $previous->month, 1, $previous->year)))) . '">&laquo; ' . '</a></td>';
} else {
$calendar_output .= "\n\t\t".'<td colspan="3" id="prev" class="pad">&nbsp;</td>';
}

$calendar_output .= "\n\t\t".'<td class="pad">&nbsp;</td>';
'</td>
<td>';
if ( $nextyear ) {
$calendar_output .= "\n\t\t".'<td colspan="3" id="next"><a href="' . get_month_link($nextyear->year, $thismonth) . '" title="' . esc_attr( sprintf(__('View posts for %1$s %2$s'), $wp_locale->get_month($nextyear->year), date('Y', mktime(0, 0 , 0, $next->month, 1, $next->year))) ) . '">&raquo; ' . '</a></td>';
} else {
$calendar_output .= "\n\t\t".'<td colspan="3" id="next" class="pad">&nbsp;</td>';
}

$calendar_output .= '
</td>
</tr>
<tr>';

$myweek = array();

for ( $wdcount = 0; $wdcount <= 6; $wdcount++ ) {
$myweek[] = $wp_locale->get_weekday( ( $wdcount + $week_begins ) % 7 );
}

foreach ( $myweek as $wd ) {
$day_name = $initial ? $wp_locale->get_weekday_initial( $wd ) : $wp_locale->get_weekday_abbrev( $wd );
$wd = esc_attr( $wd );
$calendar_output .= "\n\t\t<th scope="col" title="$wd">$day_name</th>";
}

$calendar_output .= '
</tr>
</thead>
 
Hola de nuevo. Tras muchas pruebas y esfuerzos, he conseguido añadir al Plugin Calendar por defecto de Wordpress los enlaces de año anterior y año siguiente. Por si a alguien le puede servir, ya que yo no he encontrado nada de información por internet, aquí adjunto el código que he agregado para que no se vuelvan locos buscando como yo jejeje.

Se tiene que agregar en el archivo "general-template.php" dentro de la carpeta "wp-includes". Posiblemente exista una manera más limpia de hacerlo, pero aporto lo que he sido capaz de crear por mi cuenta, ya que como digo no he encontrado ninguna información que me ayudase a realizarlo. El código que se añade es el siguiente:

// Get the next and previous month and year with at least one post
$previous = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
FROM $wpdb->posts
WHERE post_date < '$thisyear-$thismonth-01'
AND post_type = 'post' AND post_status = 'publish'
ORDER BY post_date DESC
LIMIT 1");
$previousyear = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
FROM $wpdb->posts
WHERE post_date < '$thisyear-01'
AND post_type = 'post' AND post_status = 'publish'
ORDER BY post_date DESC
LIMIT 1");
$previousmonth = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
FROM $wpdb->posts
WHERE post_date < 'm'
AND post_type = 'post' AND post_status = 'publish'
ORDER BY post_date DESC
LIMIT 1");
$next = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
FROM $wpdb->posts
WHERE post_date > '$thisyear-$thismonth-{$last_day} 23:59:59'
AND post_type = 'post' AND post_status = 'publish'
ORDER BY post_date ASC
LIMIT 1");
$nextyear = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date)+01 AS year
FROM $wpdb->posts
WHERE post_date > '$thisyear'
AND post_type = 'post' AND post_status = 'publish'
ORDER BY post_date ASC
LIMIT 1");
/* translators: Calendar caption: 1: month name, 2: 4-digit year */
$calendar_caption = _x('%1$s %2$s', 'calendar caption');
$calendar_output = '<table id="wp-calendar">
<caption>' . sprintf(
$calendar_caption,
$wp_locale->get_month( $thismonth ),
date( 'Y', $unixmonth )
) . '</caption>
<thead>
<tr>
<td>';

if ( $previousyear ) {
$calendar_output .= "\n\t\t".'<td colspan="3" id="prev"><a id="previousyear" href="' . get_month_link($previousyear->year, $thismonth) . '" title="' . esc_attr( sprintf(__('View posts for %1$s %2$s'), $wp_locale->get_month($previousyear->year), date('Y', mktime(0, 0 , 0, $previous->month, 1, $previous->year)))) . '">&laquo; ' . date('Y', mktime(0, 0 , 0, $previous->month, 1, $previousyear->year)) . '</a></td>';
} else {
$calendar_output .= "\n\t\t".'<td colspan="3" id="prev" class="pad">&nbsp;</td>';
}

$calendar_output .= "\n\t\t".'<td class="pad">&nbsp;</td>';
'</td>
<td>';
if ( $nextyear ) {
$calendar_output .= "\n\t\t".'<td colspan="3" id="next"><a id="nextyear" href="' . get_month_link($nextyear->year, $thismonth) . '" title="' . esc_attr( sprintf(__('View posts for %1$s %2$s'), $wp_locale->get_month($nextyear->year), date('Y', mktime(0, 0 , 0, $next->month, 1, $next->year))) ) . '"> ' . date('Y', mktime(0, 0 , 0, $next->month, 1, $nextyear->year)) . ' &raquo; '.' </a></td>';
} else {
$calendar_output .= "\n\t\t".'<td colspan="3" id="next" class="pad">&nbsp;</td>';
}

$calendar_output .= '
</td>
</tr>
<tr>';

$myweek = array();

for ( $wdcount = 0; $wdcount <= 6; $wdcount++ ) {
$myweek[] = $wp_locale->get_weekday( ( $wdcount + $week_begins ) % 7 );
}

foreach ( $myweek as $wd ) {
$day_name = $initial ? $wp_locale->get_weekday_initial( $wd ) : $wp_locale->get_weekday_abbrev( $wd );
$wd = esc_attr( $wd );
$calendar_output .= "\n\t\t<th scope="col" title="$wd">$day_name</th>";
}

$calendar_output .= '
</tr>
</thead>

<tfoot>
<tr>';

if ( $previous ) {
$calendar_output .= "\n\t\t".'<td colspan="3" id="prev"><a href="' . get_month_link($previous->year, $previous->month) . '" title="' . esc_attr( sprintf(__('View posts for %1$s %2$s'), $wp_locale->get_month($previous->month), date('Y', mktime(0, 0 , 0, $previous->month, 1, $previous->year)))) . '">&lt; ' . $wp_locale->get_month_abbrev($wp_locale->get_month($previous->month)) . '</a></td>';
} else {
$calendar_output .= "\n\t\t".'<td colspan="3" id="prev" class="pad">&nbsp;</td>';
}

$calendar_output .= "\n\t\t".'<td class="pad">&nbsp;</td>';

if ( $next ) {
$calendar_output .= "\n\t\t".'<td colspan="3" id="next"><a href="' . get_month_link($next->year, $next->month) . '" title="' . esc_attr( sprintf(__('View posts for %1$s %2$s'), $wp_locale->get_month($next->month), date('Y', mktime(0, 0 , 0, $next->month, 1, $next->year))) ) . '">' . $wp_locale->get_month_abbrev($wp_locale->get_month($next->month)) . ' &gt;</a></td>';
} else {
$calendar_output .= "\n\t\t".'<td colspan="3" id="next" class="pad">&nbsp;</td>';
}

$calendar_output .= '
</tr>
</tfoot>

<tbody>
<tr>';

$daywithpost = array();

// Get days with posts
$dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(post_date)
FROM $wpdb->posts WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00'
AND post_type = 'post' AND post_status = 'publish'
AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'", ARRAY_N);
if ( $dayswithposts ) {
foreach ( (array) $dayswithposts as $daywith ) {
$daywithpost[] = $daywith[0];
}
}

// See how much we should pad in the beginning
$pad = calendar_week_mod( date( 'w', $unixmonth ) - $week_begins );
if ( 0 != $pad ) {
$calendar_output .= "\n\t\t".'<td colspan="'. esc_attr( $pad ) .'" class="pad">&nbsp;</td>';
}

$newrow = false;
$daysinmonth = (int) date( 't', $unixmonth );

for ( $day = 1; $day <= $daysinmonth; ++$day ) {
if ( isset($newrow) && $newrow ) {
$calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t";
}
$newrow = false;

if ( $day == gmdate( 'j', $ts ) &&
$thismonth == gmdate( 'm', $ts ) &&
$thisyear == gmdate( 'Y', $ts ) ) {
$calendar_output .= '<td id="today">';
} else {
$calendar_output .= '<td>';
}

if ( in_array( $day, $daywithpost ) ) {
// any posts today?
$date_format = date( _x( 'F j, Y', 'daily archives date format' ), strtotime( "{$thisyear}-{$thismonth}-{$day}" ) );
/* translators: Post calendar label. 1: Date */
$label = sprintf( __( 'Posts published on %s' ), $date_format );
$calendar_output .= sprintf(
'<a href="%s" aria-label="%s">%s</a>',
get_day_link( $thisyear, $thismonth, $day ),
esc_attr( $label ),
$day
);
} else {
$calendar_output .= $day;
}
$calendar_output .= '</td>';

if ( 6 == calendar_week_mod( date( 'w', mktime(0, 0 , 0, $thismonth, $day, $thisyear ) ) - $week_begins ) ) {
$newrow = true;
}
}

$pad = 7 - calendar_week_mod( date( 'w', mktime( 0, 0 , 0, $thismonth, $day, $thisyear ) ) - $week_begins );
if ( $pad != 0 && $pad != 7 ) {
$calendar_output .= "\n\t\t".'<td class="pad" colspan="'. esc_attr( $pad ) .'">&nbsp;</td>';
}
$calendar_output .= "\n\t</tr>\n\t</tbody>\n\t</table>";

$cache[ $key ] = $calendar_output;
wp_cache_set( 'get_calendar', $cache, 'calendar' );

if ( $echo ) {
/**
* Filters the HTML calendar output.
*
* @since 3.0.0
*
* @param string $calendar_output HTML output of the calendar.
*/
echo apply_filters( 'get_calendar', $calendar_output );
return;
}
/** This filter is documented in wp-includes/general-template.php */
return apply_filters( 'get_calendar', $calendar_output );
}

/**
 
Atrás
Arriba