サポート » 使い方全般 » 投稿者別の記事一覧ができない

  • 解決済 moritokawa

    (@moritokawa)


    はじめて投稿します。初心者です。

    複数の投稿者で投稿するブログを運営しています。
    カスタム投稿で投稿ポストタイプ名を 「staffblog」としています。

    各スタッフのプロフィールページ(author.php)に
    そのスタッフが投稿した記事のみ5件表示させたく、
    以下のコードを入れています。

    <?php while ( have_posts() ) : the_post(); ?>
    
    <ul>
    <?php
    $author_id = get_the_author_meta( "ID" );
    query_posts("author=$author_id&orderby=date&post_type=staffblog&posts_per_page=5"); ?>
    <?php while (have_posts()) : the_post(); ?>
    <li>
    
        <h4><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h4><!--タイトル -->
        <div class="text2">
    	<?php the_date('Y.m.d'); ?> <!-- date -->
    	</div><br />
    		<?php if ( is_archive() || is_search() ) : // Display excerpts for archives and search. ?>
    			<div class="entry-summary">
    				<?php the_excerpt(); ?>
    			</div><!-- .entry-summary -->
    		<?php else : ?>
    			<div class="entry-content">
    				<?php the_content( __( 'more <span class="meta-nav">&rarr;</span>', 'skeleton' ) ); ?>
    			</div><!-- .entry-content -->
    
                <?php endif; ?>
    
    </li>
    <?php endwhile; ?>
    <?php wp_reset_query();?>
    </ul>
    
    <?php endwhile; ?>

    最初一人のみ投稿していた時は正常に表示されていたのですが、
    その他の投稿者が投稿しても、記事一覧部分には何も表示されません。

    どこかおかしいところがあれば教えて頂けますでしょうか。
    よろしくお願いします。

    テーマは「skeleton」を使用しています

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

    (@gatespace)

    author.php(投稿者アーカイブ)の記事のありなしはテンプレート表示前に通常の投稿のあるなしで判断されます。
    記事が出ないというユーザーで通常の投稿をしているかどうか確認してみてください。

    投稿がなかったら、query_posts 使わない pre_get_posts 使ってクエリを改変するやり方の方がベストです

    トピック投稿者 moritokawa

    (@moritokawa)

    gatespace様

    ありがとうございます!
    pre_get_postsで調べて見たところ、

    WordPressでページ送りが動かないのはどう考えてもquery_postsが悪い!【pre_get_posts、WordPressループまとめ】

    上記のページを参考にコードを直したところ、
    思うような表示がされるようになりました!

    <ul>
    <?php $args = array(
        'author'=>( $author ),
        'posts_per_page' => 5,
        'offset' => 0,
        'cat' => 0,
        'orderby' => 'post_date',
        'order' => 'DESC',
        'post_type' => 'staffblog',
        'post_status' => 'publish',
        'suppress_filters' => true,
        'ignore_sticky_posts' => true,
        'no_found_rows' => true
    );
    $the_query = new WP_Query( $args );
    if ( $the_query->have_posts() ) {
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            ?>
     <li>   <h4><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h4><!--タイトル -->
                <div class="text2">
    	<?php the_date('Y.m.d'); ?> <!-- date -->
    	</div><br />
        			<div class="entry-summary">
    				<?php the_excerpt(); ?>
    			</div><!-- .entry-summary -->
    
    </li>
            <?php
        }
    }
    wp_reset_postdata(); ?>
    </ul>

    思い通りの表示ができたので解決済みにします。
    ありがとうございました!!!!

2件の返信を表示中 - 1 - 2件目 (全2件中)
  • トピック「投稿者別の記事一覧ができない」には新たに返信することはできません。