最初と最後の記事へのリンクを出力する関数

最初と最後の記事へのリンクを出力するようにしました。

/*
 * 最初と最後の記事のリンクを出力します
 */
function print_newest_post( $indent = 1 ) {
    global $wpdb, $id;

    $indentText = '';
    for ( $i = 0; $i < $indent; $i ++ )
        $indentText .= "\t";

    $r = array( 'type' => 'postbypost', 'limit' => '1' );
    $where = apply_filters( 'getarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'", $r );
    $join = apply_filters( 'getarchives_join', "", $r );

    $infos = array (
        array( 'orderby' => 'post_date DESC ', 'linkrel' => 'last'  ), // 最後の記事
        array( 'orderby' => 'post_date ASC ',  'linkrel' => 'first' )  // 最初の記事
    );

    foreach( $infos as $info ) {
        extract( $info );
        $arcresults = $wpdb->get_results( "SELECT * FROM $wpdb->posts $join $where ORDER BY $orderby  LIMIT 1" );
        if ( $arcresults ) {
            $arcresult = $arcresults[0];
            if( $arcresult->ID != $id ) {
                echo $indentText. '<link rel="'. $linkrel. '" href="'. get_permalink($arcresult->ID). '" title="'. apply_filters('the_title', $arcresult->post_title, $arcresult). '" />'. "\n";
            }
        }
    }
}

この関数を呼ぶ前に the_post を行ってください。そうでないとエントリーIDが取得できませんので(取得できなくても大した問題はありませんが)。 wp.Vicuna デフォルトでは body 要素の前で行っているのでそれを先頭にもってきてください。

wp_get_archives 関数を参考に作成しました。

インフォメーション

公開日時
2007年9月14日 午前11時6分40秒
最終更新日時
2008年1月3日 午後7時36分23秒
カテゴリ
WordPress