如何创建动态更新的版权声明?

时间:2011-04-11 作者:ZaMoose

我想找到一种优化的、WP\\u Query-safe方法,为我的主题底部生成一个动态填充的版权声明。也就是说,我想检查我最老和最新帖子的日期(按年份),然后按照

[blog name] © [oldest post year]-[newest post year] [primary blog author]
最简单/最安全的方法是什么?

2 个回复
最合适的回答,由SO网友:Chip Bennett 整理而成

以下是我使用的:

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\\U缓存中:

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;
}
这会稍微提高性能吗?

SO网友:S. M. Mehdi Akram
/* 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 */
结束

相关推荐