Как мне создать динамически обновленный заявление об авторском праве?
Вопрос
Я хотел бы найти оптимизированный метод WP_QUERY-безопасного для создания динамически заполненного оператора авторского права для нижней части моих тем. То есть я хотел бы проверить дату (к году) моих самых старых и новейших постов, а затем вывести что -то вроде
[blog name] © [oldest post year]-[newest post year] [primary blog author]
Какой самый простой/безопасный способ сделать это?
Решение
Вот что я использую:
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;
}
Конечно, если есть более чистый, более безопасный или более эффективный метод, я бы тоже хотел услышать об этом!
РЕДАКТИРОВАТЬ:
И вот более очищенная версия, которая добавляет даты авторских прав к 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;
}
Будет ли это немного улучшить производительность?
Другие советы
/* 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 */
Не связан с wordpress.stackexchange