¿Cómo creo una declaración de derechos de autor actualizado dinámicamente?
Pregunta
Me gustaría encontrar un método optimizado y seguro WP_Query para generar una declaración de copyright poblada dinámicamente para el fondo de mis temas. Es decir, me gustaría verificar la fecha (por año) de mis publicaciones más antiguas y más nuevas y luego generar algo en la línea de
[blog name] © [oldest post year]-[newest post year] [primary blog author]
¿Cuál es la forma más fácil/segura de hacerlo?
Solución
Esto es lo que uso:
function oenology_copyright() {
global $wpdb;
$copyright_dates = $wpdb->get_results("
SELECT
YEAR(min(post_date_gmt)) AS firstdate,
YEAR(max(post_date_gmt)) AS lastdate
FROM
$wpdb->posts
WHERE
post_status = 'publish'
");
$output = '';
if($copyright_dates) {
$copyright = "© " . $copyright_dates[0]->firstdate;
if($copyright_dates[0]->firstdate != $copyright_dates[0]->lastdate) {
$copyright .= '-' . $copyright_dates[0]->lastdate;
}
$output = $copyright;
}
return $output;
}
Por supuesto, si hay un método más limpio, más seguro o más eficiente, ¡también me encantaría saberlo!
EDITAR:
Y aquí hay una versión más limpia, que agrega las fechas de derechos de autor a WP_CACHE:
function oenology_copyright() {
// check for cached values for copyright dates
$copyright_cache = wp_cache_get( 'copyright_dates', 'oenology' );
// query the database for first/last copyright dates, if no cache exists
if ( false === $copyright_cache ) {
global $wpdb;
$copyright_dates = $wpdb->get_results("
SELECT
YEAR(min(post_date_gmt)) AS firstdate,
YEAR(max(post_date_gmt)) AS lastdate
FROM
$wpdb->posts
WHERE
post_status = 'publish'
");
$copyright_cache = $copyright_dates;
// add the first/last copyright dates to the cache
wp_cache_set( 'copyright_dates', $copyright_cache, 'oenology', '604800' );
}
// Build the copyright notice, based on cached date values
$output = '© ';
if( $copyright_cache ) {
$copyright = $copyright_cache[0]->firstdate;
if( $copyright_cache[0]->firstdate != $copyright_cache[0]->lastdate ) {
$copyright .= '-' . $copyright_cache[0]->lastdate;
}
$output .= $copyright;
} else {
$output .= date( 'Y' );
}
return $output;
}
¿Esto mejoraría un poco el rendimiento?
Otros consejos
/* Dynamic Copyright Date Start */
function royaltechbd_copyright() {
global $wpdb;
$copyright_dates = $wpdb->get_results("
SELECT
YEAR(min(post_date_gmt)) AS firstdate,
YEAR(max(post_date_gmt)) AS lastdate
FROM
$wpdb->posts
WHERE
post_status = 'publish'
");
$output = '';
if($copyright_dates) {
$copyright = "© " . $copyright_dates[0]->firstdate;
if($copyright_dates[0]->firstdate != $copyright_dates[0]->lastdate) {
$copyright .= '-' . $copyright_dates[0]->lastdate;
}
$output = $copyright;
}
return $output;
}
/* Dynamic Copyright Date End */
Licenciado bajo: CC-BY-SA con atribución
No afiliado a wordpress.stackexchange