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

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

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

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

if

if文とは様々なプログラミング言語で使用される制御構文の一種であり、条件によって処理の流れを制御します。

PHP

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

Q&A

解決済

1回答

698閲覧

1度完成させたコードが消えてしまい組みなおしたがうまくいかない

homepage-site

総合スコア28

foreach

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

if

if文とは様々なプログラミング言語で使用される制御構文の一種であり、条件によって処理の流れを制御します。

PHP

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

0グッド

0クリップ

投稿2021/08/21 11:56

前提・実現したいこと

syntax error, unexpected end of fileのエラー箇所がわからない

ヘディングのテキスト

<?php if ( have_posts() ): ?> <?php while ( have_posts() ) : the_post(); ?> <article class="article__item"> <a href="<?php the_permalink(); ?>"> <h1><?php the_title(); ?></h1> <time><?php the_time(); ?></time> </a> </article> <?php ?> $wp_query = new WP_Query(); $my_posts = array( 'post_type' => "post",// 'paged' => get_query_var( 'paged', 3 ) ); $wp_query->query( $my_posts ); if( $wp_query->have_posts() ): while( $wp_query->have_posts() ) : $wp_query->the_post(); ?> <?php?> <ul> <li><a href="<?php echo get_permalink(); ?>"> <div> <?php // if ( has_post_thumbnail()) { // the_post_thumbnail(); } else { // echo '<img src="noimage.jpg">'; } ?></div> <div> <h2><?php the_title(); ?></h2></div> <div> <?php the_time('Y/m/d'); ?></div> <?php $categories = get_the_category(); if ( $categories ) { echo '<ul>'; foreach ( $categories as $category ) { echo '<li class="cat1"'.$category->slug.'"><a href="'.esc_url(get_category_link($category->term_id)).'">'.$category->name.'</a></li>'; } echo '</ul>'; } ?> <?php $num_comments = get_comments_number(); if ( $num_comments == 0 ) { $comments = __('No Comments'); // } elseif ( $num_comments > 1 ) { $comments = $num_comments . __(' Comments'); // } else { $comments = __('1 Comment'); // } $write_comments = '<span class="singlecomments"><a href="' . get_comments_link() .'">'. $comments.'</a></span>'; echo $write_comments; ?> <div> <?php the_excerpt(); ?></div> <div> <a href="<?php the_permalink(); ?>">記事を読む</a></div> </ul> <?php endwhile; ?> <nav> <?php global $wp_query; $big = 999999999; echo paginate_links( array( 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ), 'format' => '?paged=%#%', 'current' => max( 1, get_query_var('paged') ), 'total' => $wp_query->max_num_pages ) ); ?> </nav> <?php else: ?> <p>Not Found.</p> <?php endif; ?> <form method="get" id="searchform" action="<?php bloginfo('url'); ?>"> <label for="s" class="assistive-text">検索</label> <input type="text" name="s" id="s" placeholder="検索" /> <input type="submit" value="検索" /> </form> <?php get_footer(); ?>

発生している問題・エラーメッセージ

構文エラー、予期しないファイルの終わり

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

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

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

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

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

Zuishin

2021/08/21 12:00

エラー箇所はエラーメッセージに書いてあるでしょう。
homepage-site

2021/08/21 12:10

<?php ?> $wp_query = new WP_Query(); $my_posts = array( 'post_type' => "post",// 'paged' => get_query_var( 'paged', 3 ) ); $wp_query->query( $my_posts ); if( $wp_query->have_posts() ): while( $wp_query->have_posts() ) : $wp_query->the_post(); ?> からエラーが出ており、 <?php if ( have_posts() ): ?> if( $wp_query->have_posts() ): while( $wp_query->have_posts() ) : $wp_query->the_post(); とif文が2つあるのに対し ELSE ~ END IFが1つしかなく構文がきっちり閉じられていないのが原因ではないかとわかりました。 すみません。
ku__ra__ge

2021/08/21 15:54 編集

自己回答を投稿し、それをベストアンサーにして質問を終了させてください。
guest

回答1

0

自己解決

<?php /* Template Name: キタサンブラック Template Post Type: page */ ?> <?php get_header(); ?> <!--ループ--> <article class="article__item"> <a href="<?php the_permalink(); ?>"> <h1 class="ttl"><?php the_title(); ?></h1> <time class="date"><?php the_time(); ?></time> </a> </article> <?php $wp_query = new WP_Query(); $args = Array( 'post_type' => "post",//投稿タイプ設定 'posts_per_page' => 2, // 表示する投稿数(-1を指定すると全投稿を表示) ); $wp_query->query( $my_posts ); if( $wp_query->have_posts() ): while( $wp_query->have_posts() ) : $wp_query->the_post(); ?> <?php /**** ▼1件分の投稿内容HTML ****/ ?> <ul class="newarrival_list"> <li class="custom"><a href="<?php echo get_permalink(); ?>"> <div class="img-wrap"> <!--画像を追加--> <?php // アイキャッチ画像の確認 if ( has_post_thumbnail()) { // 存在する the_post_thumbnail(); } else { // 存在しない echo '<img src="noimage.jpg">'; } ?></div> <!--リンククラスつきのタイトルを追加--> <div class="img-wrap2"> <h2><?php the_title(); ?></h2></div> <!--日付けを追加-->   <div class="img-wrap3"> <?php the_time('Y/m/d'); ?></div> <!--リンククラス付きのカテゴリーを追加--> <?php $categories = get_the_category(); if ( $categories ) { echo '<ul>'; foreach ( $categories as $category ) { echo '<li class="cat1"'.$category->slug.'"><a href="'.esc_url(get_category_link($category->term_id)).'">'.$category->name.'</a></li>'; } echo '</ul>'; } ?>   <!--リンククラス付きのコメント数を追加--> <?php $num_comments = get_comments_number(); if ( $num_comments == 0 ) { $comments = __('No Comments'); // 댓글이 없을 경우 } elseif ( $num_comments > 1 ) { $comments = $num_comments . __(' Comments'); // 댓글이 2개 이상일 경우 } else { $comments = __('1 Comment'); // 댓글이 1개일 경우 } $write_comments = '<span class="singlecomments"><a href="' . get_comments_link() .'">'. $comments.'</a></span>'; echo $write_comments; ?> <!--リンククラス付きのコメントの抜粋を追加--> <div class="img-wrap4"> <?php the_excerpt(); ?></div> <!--リンククラス付きの記事を読むを追加--> <div class="img-wrap5"> <a href="<?php the_permalink(); ?>">記事を読む</a></div> </ul> <!--//ループ--> <?php endwhile; ?> <!--ページ送り--> <nav class="pagenav"> <?php global $wp_query; $big = 999999999; // need an unlikely integer echo paginate_links( array( 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ), 'format' => '?paged=%#%', 'current' => max( 1, get_query_var('paged') ), 'total' => $wp_query->max_num_pages ) ); ?> </nav> <!--//ページ送り--> <?php else: ?> <!--投稿が見つからない--> <p>Not Found.</p> <!--//投稿が見つからない--> <?php endif; ?> <form method="get" id="searchform" action="<?php bloginfo('url'); ?>"> <label for="s" class="assistive-text">検索</label> <input type="text" name="s" id="s" placeholder="検索" /> <input type="submit" value="検索" /> </form> <?php get_footer(); ?>

投稿2021/08/22 04:29

homepage-site

総合スコア28

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問