• 解決済 wtakop

    (@wtakop)


    トップページのみis_homeで分岐して
    loop.phpの投稿の最新記事5件をだしたいのですがなかなかうまくいきません。

    loop.php抜粋この部分は表示されてました。

    <?php if(have_posts()): while(have_posts()): the_post(); ?>
               <?php $cat = get_the_category(); $cat = $cat[0]; {
      echo '<li class="' . $cat->category_nicename . '" />';
     } ?>
    <?php the_category(); ?>
    <time datetime='<?php the_time('Y-m-d'); ?>'> <?php the_time('Y年m月d日'); ?></time>
                 <a href='<?php the_permalink(); ?>'><?php the_excerpt(); ?></a></li>
    <?php endwhile; endif; ?>

    分岐の記述をつけたのですが変更後表示されません。

    <?php
    if(is_home()):
    $args=array(
    	'post_type' =>'post',
    	'posts_per_page' =>2,
    	);
    	$the_query=new WP_Query($args);
    	if($the_query->have_posts()):
    ?>
    <?php if(have_posts()): while(have_posts()): the_post(); ?>
               <?php $cat = get_the_category(); $cat = $cat[0]; {
      echo '<li class="' . $cat->category_nicename . '" />';
     } ?>
    <?php the_category(); ?>
    <time datetime='<?php the_time('Y-m-d'); ?>'> <?php the_time('Y年m月d日'); ?></time>
                 <a href='<?php the_permalink(); ?>'><?php the_excerpt(); ?></a></li>
    <?php endwhile; endif; ?>

    以上です。どなたかお分かりの方いらっしゃいましたら宜しくお願いいたします。

2件の返信を表示中 - 1 - 2件目 (全2件中)
  • nobita

    (@nobita)

    こんにちは、

    function hwl_home_pagesize( $query ) {
    
        if ( is_admin() || ! $query->is_main_query() ) {
            return;
    	}
    
        if ( is_home() ) {
            // Display only 5 post for the original blog archive
            $query->set( 'posts_per_page', 5 );
            return;
        }
    }
    add_action( 'pre_get_posts', 'hwl_home_pagesize', 1 );

    詳細は、

    https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

    Changing the number of posts per page, by post type

    トピック投稿者 wtakop

    (@wtakop)

    ご連絡が遅くなりごめんなさい。御対応いただきありがとうございます。
    無事解決できました。この度はありがとうございました!

2件の返信を表示中 - 1 - 2件目 (全2件中)

トピック「トップページのみ最新記事5件出したい。」には新たに返信することはできません。