前提・実現したいこと
下記のサイトのようにメイン部分とフッター部分で新着記事の表示件数をかえたい。
参考
発生している問題・エラーメッセージ
wordpressの管理画面で表示件数の設定で変えたがどうしてもメインとフッターでリンクしてしまう。
該当のソースコード
php
1<!-- メインコンテンツ --> 2 <div class="main-contents"> 3 <div class="main-contents__box"> 4 <div class="main-contents__list"> 5 <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 6 <article class="main-contents__block"> 7 <figure class="main-contents__image"> 8 <a href="<?php the_permalink(); ?>"> 9 <?php if (has_post_thumbnail()) : ?> 10 <?php the_post_thumbnail('full'); ?> 11 <?php else : ?> 12 <img src="https://placehold.jp/600x400.png" alt=""> 13 <?php endif; ?> 14 </a> 15 </figure> 16 <section class="main-contents__texts"> 17 <p class="main-contents__time"><?php the_time('Y/m/d'); ?></p> 18 <p class="category"> <a href="<?php the_permalink(); ?>"> 19 <?php the_category(','); ?> 20 </a> 21 </p> 22 <h2> 23 <a href="<?php the_permalink(); ?>"> 24 <?php the_title(); ?> 25 </a> 26 </h2> 27 28 <p class="main-contents__description"><?php the_excerpt(); ?></p> 29 </section> 30 <p class="btn"><a href="<?php the_permalink(); ?>">READ MORE</a></p> 31 </article> 32 <?php endwhile; ?> 33 <?php endif; ?> 34 <div class="clearfix"> 35 <?php if (function_exists("pagination")) { 36 pagination($additional_loop->max_num_pages); 37 } ?> 38 </div> 39 </div> 40 //サイドバー 41<ul class="page-footer__list"> 42 <li class="page-footer__block footer-block"> 43 <h4>Recent posts</h4> 44 <?php if(have_posts()):while(have_posts()): the_post(); ?> 45 <div class="footer-block__wrapper footer-wrapper"> 46 <a href="<?php the_permalink(); ?>"> 47 <p class="footer-wrapper__image"> 48 <?php if(has_Post_thumbnail()): ?> 49 <?php the_post_thumbnail(('full')) ?> 50 <?php else:?> 51 <img src="https://placehold.jp/600x400.png" alt=""> 52 <?php endif; ?> 53 </p> 54 </a> 55 <div class="footer-wrapper__texts"> 56 <p class="date"><?php the_time('Y/m/d'); ?></p> 57 <a href=""> 58 <h5><?php the_title(); ?></h5> 59 </a> 60 </div> 61 </div> 62 <?php endwhile; ?> 63 <?php endif; ?> 64 </li> 65</ul> 66 67</div> 68</div>
php
1//追加したコード 2 <li class="page-footer__block footer-block"> 3 <h4>Recent posts</h4> 4 <?php 5 $args = array( 6 'post_type' => 'post', 7 'posts_per_page' => 3, 8 ); 9 $the_query = new WP_Query( $args ); 10 if ( $the_query->have_posts() ) : 11 while ( $the_query->have_posts() ) : $the_query->the_post(); 12 ?> 13 <!-- ループ内容 --> 14 <?php if(have_posts()):while(have_posts()): the_post(); ?> 15 <div class="footer-block__wrapper footer-wrapper"> 16 <a href="<?php the_permalink(); ?>"> 17 <p class="footer-wrapper__image"> 18 <?php if(has_Post_thumbnail()): ?> 19 <?php the_post_thumbnail(('full')) ?> 20 <?php else:?> 21 <img src="https://placehold.jp/600x400.png" alt=""> 22 <?php endif; ?> 23 </p> 24 </a> 25 <div class="footer-wrapper__texts"> 26 <p class="date"><?php the_time('Y/m/d'); ?></p> 27 <a href=""> 28 <h5><?php the_title(); ?></h5> 29 </a> 30 </div> 31 </div> 32 <?php endwhile; ?> 33 <?php endif; ?> 34 <?php 35 endwhile; 36 endif; 37 ?> 38 <?php wp_reset_postdata(); ?> 39 40 </li> 41
php
1//functions.php 2 3 4//表示数制限 5 6function my_pre_get_posts( $query ) { 7 if ( is_admin() || ! $query -> is_main_query() ) return; 8 9 if ( $query->is_home ) { 10 $query->set( 'posts_per_page', '6' ); 11 $query->set( 'orderby', 'rand' ); 12 } 13 14 if ( $query -> is_archive() ) { 15 $query -> set( 'posts_per_page', '5' ); 16 } 17 } 18 add_action( 'pre_get_posts', 'my_pre_get_posts' ); 19 20 21?> 22
試したこと
wordpressの管理画面で表示件数の設定をしたが駄目だった。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/04/29 01:26
2020/04/30 05:59