サポート » 使い方全般 » カスタム投稿タイプのアーカイブページのページング

  • 解決済 toyostyle

    (@toyostyle)


    フォーラムの中でいくつか同じような投稿を見て参考にしたのですが、なぜかうまく動作しません。宜しければご教授いただければと思います。
    投稿数は認識されていると思うのですが、2ページに移動した時も1ページ目と同じ内容になってしまいます。

    functions.phpに以下を記述してカスタム投稿タイプを作成しています。

    add_action(‘init’, ‘tblog_init’);
    function tblog_init()
    {
    $labels = array(
    ‘name’ => ‘技術ブログ’,
    ‘singular_name’ => ‘技術ブログ’,
    ‘add_new’ => ‘新規作成’,
    ‘add_new_item’ => ‘技術ブログの登録’,
    ‘edit_item’ => ‘技術ブログを編集する’,
    ‘new_item’ => ‘新しい技術ブログ’,
    ‘view_item’ => ‘技術ブログ表示’,
    ‘search_items’ => ‘技術ブログ検索’,
    ‘not_found’ => ‘技術ブログが見つかりません’,
    ‘not_found_in_trash’ => ‘ゴミ箱に技術ブログはありません’,
    ‘parent_item_colon’ => ”
    );
    $args = array(
    ‘labels’ => $labels,
    ‘public’ => true,
    ‘publicly_queryable’ => true,
    ‘show_ui’ => true,
    ‘query_var’ => true,
    ‘rewrite’ => true,
    ‘has_archive’ => true,
    ‘capability_type’ => ‘post’,
    ‘hierarchical’ => false,
    ‘menu_position’ => 5,
    // ‘supports’ => array(‘title’,’editor’,’author’,’thumbnail’,’excerpt’,’comments’)
    ‘supports’ => array(‘title’,’editor’,’page-attributes’,’comments’)
    );
    register_post_type(‘tblog’,$args);
    //カテゴリータイプ
    $args = array(
    ‘label’ => ‘技術ブログのカテゴリー’,
    ‘public’ => true,
    ‘show_ui’ => true,
    ‘hierarchical’ => true
    );
    register_taxonomy(‘tblog_category’,’tblog’,$args);
    flush_rewrite_rules( false );
    }

    次に『archive-tblog.php』専用のアーカイブページを作成しました。
    記述内容は以下になります。

    <?php
    $wp_query = new WP_Query();
    $param = array(
    ‘posts_per_page’ => ‘3’, //表示件数。-1なら全件表示
    ‘post_type’ => ‘tblog’, //カスタム投稿タイプの名称を入れる
    ‘post_status’ => ‘publish’, //取得するステータス。publishなら一般公開のもののみ
    ‘orderby’ => ‘ID’, //ID順に並び替え
    ‘order’ => ‘DESC’
    );
    $wp_query->query($param);
    if($wp_query->have_posts()): while($wp_query->have_posts()) : $wp_query->the_post();
    ?>

    <div class=”fes_post_box”>
    <p class=”mb10″>” title=”<?php the_title_attribute(); ?>”><span class=”bold font22″><?php the_title(); ?></span>
    <?php the_time(‘Y/m/d’); ?></p>
    <p class=”mb10″><img class=”top_cimg” src=”<?php echo catch_that_image(); ?>” alt=”” width=”200″ /><?php echo my_excerpt(200); ?></p>
    <p>” class=”more-link”>▼続きを読む</p>
    <div class=”clear”></div>
    </div>
    <?php endwhile; endif; ?>

    <!– /pagenav –>
    <div class=”pagenav”>

    <?php global $wp_rewrite;
    $paginate_base = get_pagenum_link(1);
    if (strpos($paginate_base, ‘?’) || ! $wp_rewrite->using_permalinks()) {
    $paginate_format = ”;
    $paginate_base = add_query_arg(‘paged’, ‘%#%’);
    } else {
    $paginate_format = (substr($paginate_base, -1 ,1) == ‘/’ ? ” : ‘/’) .
    user_trailingslashit(‘page/%#%/’, ‘paged’);;
    $paginate_base .= ‘%_%’;
    }
    echo paginate_links( array(
    ‘base’ => $paginate_base,
    ‘format’ => $paginate_format,
    ‘total’ => $wp_query->max_num_pages,
    ‘mid_size’ => 3,
    ‘current’ => ($paged ? $paged : 1),
    )); ?>

    </div>
    <!– pagenav/ –>

    記述内容などで間違っていましたらご指摘お願いします。

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

    (@gatespace)

    archive-tblog.php はそのままカスタム投稿タイプ tblog の投稿を出力しますから
    WP_Query 要らないと思うのですが。

    トピック投稿者 toyostyle

    (@toyostyle)

    早速のお返事ありがとうございます。
    まだまだWPもPHPも初心者で分からない事が多いのですが、
    ご指摘がありました『WP_Query』を無くしてみたのですが、投稿の読み込み部分は以下の記述で問題ないでしょうか?
    こちらの記述で現在ページャーが正常に動作しました。
    あと、今の状態ですと管理画面の『設定』⇒『表示設定』の『1ページに表示する最大投稿数』の件数に依存してしまうのですが、カスタム投稿タイプ別で表示件数を設定する事は可能でしょうか?
    複数のカスタム投稿タイプの設置を予定しています。
    すいませんが宜しくお願いします。

    <?php
    $param = array(
    ‘posts_per_page’ => ‘3’, //表示件数。-1なら全件表示
    ‘post_type’ => ‘tblog’, //カスタム投稿タイプの名称を入れる
    ‘post_status’ => ‘publish’, //取得するステータス。publishなら一般公開のもののみ
    ‘orderby’ => ‘ID’, //ID順に並び替え
    ‘order’ => ‘DESC’
    );
    if($wp_query->have_posts()): while($wp_query->have_posts()) : $wp_query->the_post();
    ?>

    <div class=”fes_post_box”>
    <p class=”mb10″>
    ” title=”<?php the_title_attribute(); ?>”>
    <span class=”bold font22″><?php the_title(); ?></span>

    <?php the_time(‘Y/m/d’); ?>
    </p>
    <p class=”mb10″>
    <img class=”top_cimg” src=”<?php echo catch_that_image(); ?>” alt=”” width=”200″ />
    <?php echo my_excerpt(200); ?>
    </p>
    <p>” class=”more-link”>▼続きを読む</p>
    <div class=”clear”></div>
    </div>
    <?php endwhile; endif; ?>

    モデレーター gatespace

    (@gatespace)

    <?php
    $param = array(
    'posts_per_page' => '3', //表示件数。-1なら全件表示
    'post_type' => 'tblog', //カスタム投稿タイプの名称を入れる
    'post_status' => 'publish', //取得するステータス。publishなら一般公開のもののみ
    'orderby' => 'ID', //ID順に並び替え
    'order' => 'DESC'
    );
    if($wp_query->have_posts()): while($wp_query->have_posts()) : $wp_query->the_post();
    ?>

    まずはページ送りが正しく動くかどうか。
    この辺の記述がいらなかったり無駄だったりするので、デフォルトテーマのarchive.phpを参考に書き直してみてください。

    そこまで出来たら、カスタム投稿タイプ毎にページ送りの投稿数を変えるのであれば
    pre_get_posts 使った方がいいんので、
    http://notnil-creative.com/blog/archives/1996
    この辺参考にコード書いてみてください

    トピック投稿者 toyostyle

    (@toyostyle)

    『archive.php』をもとにアーイブページを修正しまして、
    参考サイトのコードを使い、無事表示件数の変更もできました。

    色々と分かりやすく説明していただいて、本当にありがとうございました!
    また質問させていただくと思いますが、宜しくお願いします。

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