Domanda

Mi piacerebbe trovare una ottimizzata, metodo di WP_Query-safe per generare una dichiarazione di copyright dinamicamente popolata per il fondo dei miei temi. Vale a dire, mi piacerebbe controllare la data (per anno) del mio più vecchio e messaggi più recenti e quindi l'uscita qualcosa lungo le linee di

[blog name] © [oldest post year]-[newest post year] [primary blog author]

Qual è il / modo più sicuro più semplice per farlo?

È stato utile?

Soluzione

Ecco quello che io 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;
}

Naturalmente, se c'è un più pulito, più sicuro, o un metodo più efficiente, mi piacerebbe sentire su di esso, anche!

EDIT:

Ed ecco una versione più pulita-up, che aggiunge le date di copyright per 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;
}

Sarebbe questo migliorare le prestazioni un po '?

Altri suggerimenti

/* 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 */
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a wordpress.stackexchange
scroll top