前提・実現したいこと
Wordoressのindexページに複数のループを置きたく実装しています。
発生している問題・エラーメッセージ
それぞれ単体で置いてみるとうまく表示されるのですが、同時に置くとうまくいきません。
index.phpで
ループが含まれている single-top_image.php をinclude。
そのあとに、メインのループが記述されています。
メインのループではcontent.phpを呼び出しています。
single-top_image.phpのループは正常に表示されますが、
メインのループでエラーが表示されます。
試したこと
また試しに、
メインのループのあとにsingle-top_image.phpをinclude配置すると、どちらも期待通りの表示がなされます。
該当のソースコード
index.php
php
1<?php get_header(); ?> 2 3 <section class="l-firstView" id="firstView"> 4 <?php include('single-top_image.php'); ?> 5 </section> 6 7 <section class="l-item" id="item"> 8 <div class="m-item"> 9 <h1 class="m-item-title"> 10 Item 11 </h1> 12 <div class="m-item-box"> 13 <ul class="m-item-list"> 14 15 <?php if ( have_posts() ) : ?> 16 17 <?php /* Start the Loop */ ?> 18 <?php while ( have_posts() ) : the_post(); ?> 19 20 <li class="m-item-listItem"> 21 <?php 22 /* Include the Post-Format-specific template for the content. 23 * If you want to override this in a child theme, then include a file 24 * called content-___.php (where ___ is the Post Format name) and that will be used instead. 25 */ 26 get_template_part( 'content', get_post_format() ); 27 ?> 28 </li> 29 30 <?php endwhile; ?> 31 32 <?php the_posts_navigation(); ?> 33 34 <?php else : ?> 35 else 36 <?php get_template_part( 'content', 'none' ); ?> 37 38 <?php endif; ?> 39 40 </ul> 41 </div> 42 </div> 43 </section> 44 45 46<?php get_footer(); ?> 47
single-top_image.php
<div class="m-firstView"> <div class="swiper-container m-firstView-swiper-container"> <div class="swiper-wrapper m-firstView-swiper-wrapper"> <?php $args = array( 'numberposts' => 0, //表示(取得)する記事の数 'post_type' => 'top_image' //投稿タイプの指定 ); $posts = get_posts( $args ); if( $posts ) : foreach( $posts as $post) : setup_postdata( $post ); ?> <div class="swiper-slide m-firstView-swiper-slide"> <a href="" class="swiper-slide-link"></a> <?php $image_id = get_post_thumbnail_id(); $image_url = wp_get_attachment_image_src($image_id, true); ?> <div class="m-firstView-swiper-slide-image-outer" style="background-image: url(<?php echo $image_url[0]; ?>)"> <img src="<?php echo $image_url[0]; ?>" alt="" class="m-firstView-swiper-slide-image"> </div> </div> <?php endforeach; ?> <?php else : //記事が無い場合 ?> <li><p>記事はまだありません。</p></li> <?php endif; ?> <?php wp_reset_postdata(); ?> </div> </div> </div>
content.php
<?php if (has_post_thumbnail( $post->ID ) ): ?> <?php // 記事のカテゴリー情報を取得する // $cat = get_the_category(); // 取得した配列から必要な情報を変数に入れる // $cat_name = $cat[0]->cat_name; // カテゴリー名 // $cat_slug = $cat[0]->category_nicename; // カテゴリースラッグ ?> <?php // 記事のタグ情報を取得する $posttags = get_the_tags(); ?> <artcile class="m-item-listItem-box <?php if ( $posttags ) { foreach ( $posttags as $tag ) { echo $tag->slug . ' ';} } ?>"> <i class="m-item-listItem-tag item-tag"> <?php $homeurl = home_url(); if ($posttags) { foreach($posttags as $tag) { echo '<a href="' . $homeurl . '/archives/tag/' . $tag->slug . '" class="' . $tag->slug . '">' . $tag->name . '</a>'; } } ?> </i> <div class="m-item-listItem-img-box"> <img src="<?php echo wp_get_attachment_url( get_post_thumbnail_id( $post->ID) , 'max-control' ); ?>" alt="" class="m-item-listItem-img"> </div> <?php endif; ?> <div class="m-item-listItem-text"> <h1 class="m-item-listItem-title"> <?php the_title(); ?> </h1> <p class="m-item-listItem-price"> ¥<?php echo number_format(post_custom('価格'));?> </p> </div> <a href="<?php the_permalink(); ?>" class="m-item-listItem-link"></a> </artcile>
メインループに表示されるエラー
Notice: Undefined offset: 2 in /var/www/html/wp-includes/class-wp-query.php on line 3071 Call Stack # Time Memory Function Location 1 0.0031 357416 {main}( ) .../index.php:0 2 0.0118 357704 require( '/var/www/html/wp-blog-header.php' ) .../index.php:17 3 1.3411 6177224 require_once( '/var/www/html/wp-includes/template-loader.php' ) .../wp-blog-header.php:19 4 1.3524 6182520 include( '/var/www/html/wp-content/themes/silhouette/index.php' ) .../template-loader.php:74 5 1.5265 6288368 the_post( ) .../index.php:26 6 1.5265 6288368 WP_Query->the_post( ) .../query.php:805 7 1.5265 6288368 WP_Query->next_post( ) .../class-wp-query.php:3099
回答2件
あなたの回答
tips
プレビュー