動的に設定された著作権ステートメントを作成するにはどうすればよいですか?

wordpress.stackexchange https://wordpress.stackexchange.com/questions/14492

  •  16-10-2019
  •  | 
  •  

質問

テーマの下部に動的に人口の著作権ステートメントを生成するための最適化されたwp_query-safeメソッドを見つけたいと思います。つまり、私は私の最も古くて最新の投稿の日付(年ごと)をチェックしてから、その行に沿って何かを出力したいと思います

[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 */
ライセンス: CC-BY-SA帰属
所属していません wordpress.stackexchange
scroll top