質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
foreach

foreachは、List・Collection・Arrayといったデータ構造の各要素に対して繰り返し処理を実行するために扱われる、制御構造の構文です。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Q&A

解決済

2回答

968閲覧

foreach分のシンタックスエラーについて

hirohiro121212

総合スコア15

foreach

foreachは、List・Collection・Arrayといったデータ構造の各要素に対して繰り返し処理を実行するために扱われる、制御構造の構文です。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

0グッド

0クリップ

投稿2019/09/27 02:58

編集2019/09/27 04:33

以下のコードで、シンタックスエラーが出るのですが、
php初心者なので、どこがエラーなのかわかりません。。(x x;)

php

1 2<?php 3$taxonomyName = "shain_category"; 4$args = array('parent' => 0); 5$terms = get_terms($taxonomyName,$args); 6foreach ($terms as $term) { 7 echo "<h2 style='font-size:30px;margin:20px 0;'>$term->name</h2>"; 8 $parentId = $term->term_id; 9 $childargs = array( 10 'parent' => $parentId, 11 'hide_empty' => false//投稿がない場合も隠さずにだす 12 ); 13 $childterms = get_terms($taxonomyName,$childargs); 14 foreach ($childterms as $childterm) { 15 $targetSlug = $childterm->slug; 16 echo "<h2 style='font-size:16px;margin:10px 0;'>$childterm->name</h2>";//子タームの名前 17 echo '<ul>'; 18 foreach($myPost as $post) : setup_postdata( $post ); 19 ?> 20 21 <li> 22 <div class="shinloopList"> 23 <a href="<?php the_permalink(); ?>"> 24 25 <?php 26 $attachment_id = get_field('shain_photo'); 27 $size = "shain-thumbnails"; // (thumbnail, medium, large, full or custom size) 28 $image = wp_get_attachment_image_src( $attachment_id, $size ); 29 $attachment = get_post( get_field('shain_photo') ); 30 $alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true); 31 $image_title = $attachment->post_title; 32 ?> 33 <div class="shainListimg"><figure> 34 <?php $customfield = get_post_meta($post->ID, 'shain_photo', true); ?> 35 <?php if( empty($customfield) ): ?> 36 <img src="<?php bloginfo('template_directory');?>/images/staff_noimage510.jpg" alt=""> <!-- カスタムフィールドが空だった場合にここが表示される --> 37 <?php else: ?> 38 <img src="<?php echo $image[0]; ?>" width="<?php echo $image[1]; ?>" height="<?php echo $image[2]; ?>" alt="<?php echo $alt; ?>" class="transform01" /> <!-- カスタムフィールドの値が入力されていたらここに表示される --> 39 <?php endif; ?> 40 </figure></div> 41 42 <div class="shainListTxt"> 43 <p class="shain_name"><?php the_field('shain_name');?></p> 44 <p class="shain_kaisha_archive"><?php the_field('shain_yakushoku');?></p> 45 <p class="shain_noujou_archive"><?php the_field('shain_noujou');?></p> 46 </div><!-- shainListTxt --> 47 48 </a> 49 </div><!-- shinloopList --> 50 </li> 51 52 <? 53 endforeach; 54 endif; 55 echo '</ul>'; 56 wp_reset_postdata(); 57 } 58 59 60 61 62?>

最後のendif;のところがエラーだといわれるのですが、わかりません!!汗;

皆様、沢山のご回答をありがとうございます。
まだ解決していないのですが、、

それは、シンタックスエラーは出なくなったのですが、

子タームに登録している内容の表示が、一番最後の子タームのものが全部表示されてしまいます。

文章で説明するのが下手で大変申し訳ございません!!m(_ _)m

※そこで、上手く伝えるのが難しいので、、
子ターム(タクソノミー?)それぞれの一覧を表示させるループをどうやって書けばいいのかお教えいただけましたら幸いでございます。。m(_ _)m

質問の仕方が下手で大変申し訳ありません。。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kei344

2019/09/27 03:00

エラー文を具体的にコードブロックで質問文に追記ください。
guest

回答2

0

ベストアンサー

代替構文…あったなぁ…

php

1 <? 2 endforeach; 3 endif; 4 echo '</ul>'; 5 wp_reset_postdata(); 6 }

<?

<?php
(設定によってはあってるのでなんとも言えない)

endif;は上の方でもうあるのでいらない

foreach(...){が2つあるので}が一個足りない

php

1<?php 2$taxonomyName = "shain_category"; 3$args = array('parent' => 0); 4$terms = get_terms($taxonomyName,$args); 5foreach ($terms as $term) { 6 echo "<h2 style='font-size:30px;margin:20px 0;'>$term->name</h2>"; 7 $parentId = $term->term_id; 8 $childargs = array( 9 'parent' => $parentId, 10 'hide_empty' => false//投稿がない場合も隠さずにだす 11 ); 12 $childterms = get_terms($taxonomyName,$childargs); 13 foreach ($childterms as $childterm) { 14 $targetSlug = $childterm->slug; 15 echo "<h2 style='font-size:16px;margin:10px 0;'>$childterm->name</h2>";//子タームの名前 16 echo '<ul>'; 17 foreach($myPost as $post) : setup_postdata( $post ); 18 ?> 19 20 <li> 21 <div class="shinloopList"> 22 <a href="<?php the_permalink(); ?>"> 23 24 <?php 25 $attachment_id = get_field('shain_photo'); 26 $size = "shain-thumbnails"; // (thumbnail, medium, large, full or custom size) 27 $image = wp_get_attachment_image_src( $attachment_id, $size ); 28 $attachment = get_post( get_field('shain_photo') ); 29 $alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true); 30 $image_title = $attachment->post_title; 31 ?> 32 <div class="shainListimg"><figure> 33 <?php $customfield = get_post_meta($post->ID, 'shain_photo', true); ?> 34 <?php if( empty($customfield) ): ?> 35 <img src="<?php bloginfo('template_directory');?>/images/staff_noimage510.jpg" alt=""> <!-- カスタムフィールドが空だった場合にここが表示される --> 36 <?php else: ?> 37 <img src="<?php echo $image[0]; ?>" width="<?php echo $image[1]; ?>" height="<?php echo $image[2]; ?>" alt="<?php echo $alt; ?>" class="transform01" /> <!-- カスタムフィールドの値が入力されていたらここに表示される --> 38 <?php endif; ?> 39 </figure></div> 40 41 <div class="shainListTxt"> 42 <p class="shain_name"><?php the_field('shain_name');?></p> 43 <p class="shain_kaisha_archive"><?php the_field('shain_yakushoku');?></p> 44 <p class="shain_noujou_archive"><?php the_field('shain_noujou');?></p> 45 </div><!-- shainListTxt --> 46 47 </a> 48 </div><!-- shinloopList --> 49 </li> 50 51 <?php 52 endforeach; 53 echo '</ul>'; 54 wp_reset_postdata(); 55 } 56} 57

でとりあえずシンタックスエラーは出なくなった

投稿2019/09/27 03:11

rururu3

総合スコア5545

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hirohiro121212

2019/09/27 06:10

ご教授をありがとうございます。 確かにシンタックスエラーはなくなりました。 ただ、元々の自分の記述がおかしかったので、希望通りの表示結果にはなりませんでした。 なので、違う方法(特定のターム毎の一覧のループを複数置くことで解決いたしました。)
guest

0

endforeachのあとのendifに対応するifがない。
foreachが3つあるのにendforeachが1つしかない。

投稿2019/09/27 03:08

編集2019/09/27 03:09
退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hirohiro121212

2019/09/27 06:08

回答をありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問