Question

Je voudrais trouver un optimisé, la méthode WP_Query de sécurité pour générer une déclaration de droits d'auteur dynamiquement peuplé pour le fond de mes thèmes. C'est-à-dire, je voudrais vérifier la date (par année) de mes plus anciens et les plus récents messages et afficher quelque chose le long des lignes de

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

Quelle est la meilleure façon de le faire / la plus sûre ainsi?

Était-ce utile?

La solution

Voici ce que j'utilise:

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;
}

Bien sûr, s'il y a un plus propre, ou une méthode plus efficace, je serais ravi d'en entendre parler, aussi!

EDIT:

Et voici une version plus assainis, qui ajoute les dates de copyright à 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;
}

Serait-ce d'améliorer les performances un peu?

Autres conseils

/* 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 */
Licencié sous: CC-BY-SA avec attribution
Non affilié à wordpress.stackexchange
scroll top