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

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

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

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

Q&A

解決済

1回答

1163閲覧

WordPressのエラーメッセージの消し方について

shiratama_0826

総合スコア11

PHP

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

0グッド

0クリップ

投稿2020/07/26 06:30

編集2020/07/26 10:10

前提・実現したいこと

MAMPを用いて静的サイトのWordPress化を試みています。
トップページに新着情報を掲載したく作業していたところ
ページにエラーメッセージが表示されるようになりました。

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

Warning: count(): Parameter must be an array or an object that implements Countable in /Applications/MAMP/htdocs/wp-includes/post-template.php on line 317 Notice: Trying to access array offset on value of type null in /Applications/MAMP/htdocs/wp-includes/post-template.php on line 323

該当のソースコード

HTML

1 <div class="news_box"> 2   <?php $posts = get_posts('numberposts=2'); ?> 3   <?php foreach($posts as $post): ?> 4   <a href="<?php the_permalink(); ?>"> 5   <?php the_post_thumbnail(array(250,250), array('class' => 'left')); ?></a> 6 <div class="news_box_text"> 7   <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3> 8   <p class="time"><?php the_time('Y年m月d日(D)'); ?></p> 9   <p><?php the_content(); ?></p> 10     /div> 11   <?php endforeach; ?> 12    </div>

PHP

1 2//post-template.phpの317行目と323行〜をコピぺしています 3// If the requested page doesn't exist. 4 if ( $elements['page'] > count( $elements['pages'] ) ) { 5 // Give them the highest numbered page that DOES exist. 6 $elements['page'] = count( $elements['pages'] ); 7 } 8 9 $page_no = $elements['page']; 10 $content = $elements['pages'][ $page_no - 1 ]; 11 if ( preg_match( '/<!--more(.*?)?-->/', $content, $matches ) ) { 12 if ( has_block( 'more', $content ) ) { 13 // Remove the core/more block delimiters. They will be left over after $content is split up. 14 $content = preg_replace( '/<!-- /?wp:more(.*?) -->/', '', $content ); 15 } 16 17 $content = explode( $matches[0], $content, 2 ); 18 19 if ( ! empty( $matches[1] ) && ! empty( $more_link_text ) ) { 20 $more_link_text = strip_tags( wp_kses_no_null( trim( $matches[1] ) ) ); 21 } 22 23 $has_teaser = true; 24 } else { 25 $content = array( $content ); 26 } 27 28 if ( false !== strpos( $_post->post_content, '<!--noteaser-->' ) && ( ! $elements['multipage'] || 1 == $elements['page'] ) ) { 29 $strip_teaser = true; 30 } 31 32 $teaser = $content[0]; 33 34 if ( $elements['more'] && $strip_teaser && $has_teaser ) { 35 $teaser = ''; 36 } 37 38 $output .= $teaser; 39 40 if ( count( $content ) > 1 ) { 41 if ( $elements['more'] ) { 42 $output .= '<span id="more-' . $_post->ID . '"></span>' . $content[1]; 43 } else { 44 if ( ! empty( $more_link_text ) ) { 45 46 /** 47 * Filters the Read More link text. 48 * 49 * @since 2.8.0 50 * 51 * @param string $more_link_element Read More link element. 52 * @param string $more_link_text Read More text. 53 */ 54 $output .= apply_filters( 'the_content_more_link', ' <a href="' . get_permalink( $_post ) . "#more-{$_post->ID}\" class=\"more-link\">$more_link_text</a>", $more_link_text ); 55 } 56 $output = force_balance_tags( $output ); 57 } 58 } 59 60 return $output; 61} 62

試したこと

phpのバージョンが関係していることや、
count関数で引っかかっているであろうことは調べてわかり、
修正を試みましたがうまく動作しませんでした。

【追記】
・HTMLのコードを追記しました。
・試したこと

  • count関数をis_countable関数に変えてみた
  • !empty($pages)を足してみた

恐れ入りますが、お知恵をお借りできれば幸いです。
よろしくお願いいたします。

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

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

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

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

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

hayato7

2020/07/26 07:21

どういう修正を試みられたのでしょうか? またトップページのソースコードも貼り付けていただければと思います。
shiratama_0826

2020/07/26 10:10

失礼しました。追記いたしました。よければ再読いただければ幸いです。
guest

回答1

0

ベストアンサー

<?php foreach($posts as $post): ?>の直後に

<?php setup_postdata($post); ?>
を記述してみても変化ないでしょうか?

the_content()等の関数を使う場合はsetup_postdata()による初期化が必要です。

テンプレートタグ/get posts

投稿2020/07/27 02:40

Eggpan

総合スコア2727

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

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

shiratama_0826

2020/07/27 04:15

エラーが出なくなりました!ありがとうございます! 貼っていただいたリンク先をよく読み勉強を続けます。 本当にありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問