サポート » 使い方全般 » カスタム投稿の年別アーカイブの表示について

  • 解決済 keynyon

    (@keynyon)


    初めまして、wordpress初心者です。
    カスタム投稿で作成した記事のアーカイブを年別で
    下記のように表示させたいのですが、方法がわかりません。

    ■2015年
    2015.xx.xx ブログタイトル
    2015.xx.xx ブログタイトル
    2015.xx.xx ブログタイトル
    2015.xx.xx ブログタイトル

    ■2014年
    2014.xx.xx ブログタイトル
    2014.xx.xx ブログタイトル
    2014.xx.xx ブログタイトル
    2014.xx.xx ブログタイトル

    ■2013年


    サイドバーに年を表示させて、その年のアーカイブに飛ばす方法は見つかったのですが、上記のような形で表示する方法がわかりませんでした。
    記事は最新のものから順に全件表示させたいです。

    カスタム投稿生成のために、functions.phpには以下のように記載しています。

    add_action(‘init’, ‘register_posttype’);
    function register_posttype() {
    $labels = array(‘name’=>’ブログ’,’singular_name’=>’blog’);
    $args = array(
    ‘labels’ => $labels,
    ‘public’ => true,
    ‘query_var’ => true,
    ‘rewrite’ => true,
    ‘capability_type’ => ‘page’,
    ‘has_archive’ => true,
    ‘hierarchical’ => true,
    ‘menu_position’ => 21,
    ‘supports’ => array(‘title’,’editor’,’excerpt’,’custom-fields’,’revisions’)
    );
    register_post_type(‘blog’,$args);
    }

    テンプレートファイルは、archive-blog.phpです。

    どなたかご教示いただけますようどうぞお願いいたします。

4件の返信を表示中 - 1 - 4件目 (全4件中)
  • 現在の archive-blog.php では、どのように表示できていますか? ページのURLを示していただけると話が早いです。

    それから、使っているテーマを教えてください。自作テーマの場合は archive-blog.php のコードを pastebin などへ貼って見せて頂ければと思います。

    トピック投稿者 keynyon

    (@keynyon)

    gblsm様
    ご返信ありがとうございます。
    申し訳ございませんが、依頼されたものなのでURLの添付は控えさせていただきます。

    テーマは自作です。
    書き方がまったくわからないので、現在は普通にループを回して全件を出力しています。
    年は今年の年の出力しかわかっていません。ソースは以下のとおりです。

    <div>
    	<h2">■<?php echo date('Y'); ?>年</h2>
    	<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    	<article>
    		<time datetime="<?php the_time('Y-m-d'); ?>"><?php the_time('Y.m.d'); ?></time>
    		<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    	</article>
    	<?php endwhile ?>
    	<?php else : ?>
    		<p>現在お知らせはありません</p>
    	<?php endif; ?>
    </div>

    ですので、表示は以下のとおりになっております。
    ■2015年
    2015.xx.xx ブログタイトル
    2015.xx.xx ブログタイトル
    2015.xx.xx ブログタイトル
    2014.xx.xx ブログタイトル
    2014.xx.xx ブログタイトル
    2014.xx.xx ブログタイトル

    どうぞよろしくお願いいたします。

    これでどうでしょう?(実際に動作させていません)

    <div>
    <?php if ( have_posts() ) :
        $lastyear = NULL;
        while ( have_posts() ) : the_post();
            $thisyear = get_the_date( 'Y年' );
            // 年が変わったときだけ年の見出しを出力
            if ( $thisyear != $lastyear ) : ?>
        <h2>■<? echo $thisyear; ?></h2>
            <?php endif;
            $lastyear = $thisyear; ?>
        <article>
            <time datetime="<?php the_time('Y-m-d'); ?>"><?php the_time('Y.m.d'); ?></time>
            <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
        </article>
        <?php endwhile;
    else : ?>
        <p>現在お知らせはありません</p>
    <?php endif; ?>
    </div>

    トピック投稿者 keynyon

    (@keynyon)

    gblsm様

    ご返信ありがとうございます。
    思ったとおりに出力できました!
    本当に助かりました。
    お忙しいところご教示いただきまして、誠にありがとうございます。

4件の返信を表示中 - 1 - 4件目 (全4件中)
  • トピック「カスタム投稿の年別アーカイブの表示について」には新たに返信することはできません。