サポート » テーマ » カスタム投稿で作成したカスタム分類を同一ページ内に読み込みたい

  • 解決済 katie116

    (@katie116)


    初心者です。カスタム投稿で今まで開催したイベントを投稿できるようにしていて、カスタム分類で年別に分けています。
    カテゴリー=平成16年→スラッグ=h16のような感じです。
    現在平成16年~平成23年分あります。

    これを固定ページの中でカテゴリーごとに呼び出したいのですが(タブで切替表示するので)、今後カスタム投稿の管理画面側でカテゴリーが追加されると自動で出力ページ先にも表示されるようにしたいです。

    現在はタブの部分はカスタムメニューで追加・出力されるようにしたのですが内容の部分が年別に自動で出力されないのでどうしようかと思っています。

    今現在のページ出力用のテンプレートはこのようにコードを書いてるのですが

    <?php wp_nav_menu(array(
    						'container' => '',
    						'menu_class' => 'tab'
    						)); ?> 
    
    <hr />
    
    <div id="content">
    <dl class="area" id="h23">
    <?php query_posts( array( 'frontiercat' => 'h23', 'showposts' => 100 ) ); ?>
    <?php if(have_posts()):
    while(have_posts()): the_post(); ?>
    <dt><?php the_title(); ?></dt>
    <dd><?php the_content(); ?></dd>
    <?php endwhile; endif; ?>
    </dl>
    <dl class="area" id="h22">
    <?php query_posts( array( 'frontiercat' => 'h22', 'showposts' => 100 ) ); ?>
    <?php if(have_posts()):
    while(have_posts()): the_post(); ?>
    <dt><?php the_title(); ?></dt>
    <dd><?php the_content(); ?></dd>
    <?php endwhile; endif; ?>
    </dl>
    <dl class="area" id="h21">
    <?php query_posts( array( 'frontiercat' => 'h21', 'showposts' => 100 ) ); ?>
    <?php if(have_posts()):
    while(have_posts()): the_post(); ?>
    <dt><?php the_title(); ?></dt>
    <dd><?php the_content(); ?></dd>
    <?php endwhile; endif; ?>
    </dl>
    <dl class="area" id="h20">
    <?php query_posts( array( 'frontiercat' => 'h20', 'showposts' => 100 ) ); ?>
    <?php if(have_posts()):
    while(have_posts()): the_post(); ?>
    <dt><?php the_title(); ?></dt>
    <dd><?php the_content(); ?></dd>
    <?php endwhile; endif; ?>
    </dl>
    <dl class="area" id="h19">
    <?php query_posts( array( 'frontiercat' => 'h19', 'showposts' => 100 ) ); ?>
    <?php if(have_posts()):
    while(have_posts()): the_post(); ?>
    <dt><?php the_title(); ?></dt>
    <dd><?php the_content(); ?></dd>
    <?php endwhile; endif; ?>
    </dl>
    <dl class="area" id="h18">
    <?php query_posts( array( 'frontiercat' => 'h18', 'showposts' => 100 ) ); ?>
    <?php if(have_posts()):
    while(have_posts()): the_post(); ?>
    <dt><?php the_title(); ?></dt>
    <dd><?php the_content(); ?></dd>
    <?php endwhile; endif; ?>
    </dl></div>

    この

    <dl class="area" id="h23">
    <?php query_posts( array( 'frontiercat' => 'h23', 'showposts' => 100 ) ); ?>
    <?php if(have_posts()):
    while(have_posts()): the_post(); ?>
    <dt><?php the_title(); ?></dt>
    <dd><?php the_content(); ?></dd>
    <?php endwhile; endif; ?>
    </dl>

    の部分をカテゴリーごとに取得してループで表示することは可能でしょうか?

    ずっとデザインをメインで作ってきたので、PHPは勉強を始めたばかりです。

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

4件の返信を表示中 - 1 - 4件目 (全4件中)
  • モデレーター jim912

    (@jim912)

    katie116さん、こんにちは。

    get_termsでカスタム分類を取得して、foreachでループさせましょう。

    新しい年度が最初になるようなので、get_termsのorderbyパラメーターをslug、orderをdescにすればいけると思います。

    トピック投稿者 katie116

    (@katie116)

    jim912さま

    ご回答ありがとうございます!

    現在functions.phpで

    function add_frontier_type() {
    	$frontier = array(
    				   'label' => 'フロンティアパーティー',
    				   'labels' => array(
    									 'singular_name' => 'フロンティアパーティー',
    									 'add_new_item' => 'フロンティアパーティー開催を投稿する',
    									 'add_new' => '投稿',
    									 'view_item' => '投稿を見る',
    									 'not_found' => '投稿が見つかりませんでした',
    									 'not_found_in_trash' => 'ゴミ箱に投稿はありません',
    									 'search_items' => '投稿を検索',
    									 ),
    				   'public' => true,
    				   'hierarchical' => false,
    				   'menu_position' => 5,
    				   'rewrite' => false,
    				   'supports' => array('title','editor','author','thumbnail','excerpt','comments','custom-fields')
    				   );
    	register_post_type('frontier' , $frontier);
    	flush_rewrite_rules();
    
    	register_taxonomy(
    		'frontiercat',
    		'frontier',
    		array(
    			  'label' => '開催日時管理',
    			  'hierarchical' => true,
    			  'singular_label' => '開催日時管理',
    			  'show_ui' => true,
    			  'public' => true
    			  )
    		);
    
    }
    
    add_action('init' , 'add_frontier_type');

    とカスタム投稿を定義していて

    ページテンプレートに

    <div id="content">
    <?php
        $taxonomys = get_terms( "frontiercat");
        foreach($taxonomys as $value):
     ?>
     <dl id="<?php echo $value->slug; ?>" class="area">
    <?php query_posts( array( 'frontiercat' => '$value->slug;', 'orderby' => 'slug' , 'order' => 'desc' ) ); ?>
    <?php if(have_posts()):
    while(have_posts()): the_post(); ?>
    <dt><?php the_title(); ?></dt>
    <dd><?php the_content(); ?></dd>
    <?php endwhile; endif; ?>
     </dl>
    <?php endforeach; ?>
    </div>

    とコードを書いたのですが<dl>のidとしてカテゴリのスラッグは読み込むのですが、記事タイトルと記事内容が読み込まれず真っ白になってしまいます・・・。
    <?php query_posts( array( 'frontiercat' => '$value->slug;', 'orderby' => 'slug' , 'order' => 'desc' ) ); ?>
    この’frontiercat’ => ‘$value->slug;’の部分が悪いんだとは思うのですが、これはどのように書けばカテゴリ毎の投稿を読み込んでくれるのでしょうか?

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

    トピック投稿者 katie116

    (@katie116)

    すみません上記の件は
    <?php query_posts( array( 'frontiercat' => $value->slug, 'orderby' => 'slug' , 'order' => 'DESC' ) ); ?>
    と書き自己解決できました!

    ですが平成23年度分から表示させたいのですがどうしても16年からになってしまいます。
    ‘order’ => ‘DESC’と書いたのですがなぜでしょうか?

    トピック投稿者 katie116

    (@katie116)

    すみません上記も自己解決です。。。
    書く場所を間違えていたようです。

    <div id="content">
    <?php
        $taxonomys = get_terms( "frontiercat","orderby=slug&order=DESC");
        foreach($taxonomys as $value):
     ?>
     <dl id="<?php echo $value->slug; ?>" class="area">
    <?php query_posts( array( 'frontiercat' => $value->slug, 'order'=>'ASC' ) ); ?>
    <?php if(have_posts()):
    while(have_posts()): the_post(); ?>
    <dt><?php the_title(); ?></dt>
    <dd><?php the_content(); ?></dd>
    <?php endwhile; endif; ?>
     </dl>
    <?php endforeach; ?>
    </div>

    で思い通りの表示になりました。

    ありがとうございました。

4件の返信を表示中 - 1 - 4件目 (全4件中)
  • トピック「カスタム投稿で作成したカスタム分類を同一ページ内に読み込みたい」には新たに返信することはできません。