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

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

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

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

WordPress

WordPressは、PHPで開発されているオープンソースのブログソフトウェアです。データベース管理システムにはMySQLを用いています。フリーのブログソフトウェアの中では最も人気が高く、PHPとHTMLを使って簡単にテンプレートをカスタマイズすることができます。

Q&A

解決済

1回答

1040閲覧

$counterの文法エラー原因がわからない

homepage-site

総合スコア28

if

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

WordPress

WordPressは、PHPで開発されているオープンソースのブログソフトウェアです。データベース管理システムにはMySQLを用いています。フリーのブログソフトウェアの中では最も人気が高く、PHPとHTMLを使って簡単にテンプレートをカスタマイズすることができます。

0グッド

1クリップ

投稿2021/08/23 10:16

前提・実現したいこと

RSSと画像を交互に表示させたい

ヘディングのテキスト

<!--ループ--> <?php $args = array( 'post_type' => 'post', 'posts_per_page' => 3); $query = new WP_Query( $args ); // ループ if (have_posts()) :?> <?php if(have_posts()) : ?> <section> <?php $counter = 0; while($pickUpData->have_posts()): $pickUpData->the_post(); ?> <article> <?php echo $counter; ?>番目の記事 </article> <?php //echo $counter; ?> <?php if($counter %3 == 2 && !empty($counter)) : ?> </section> <?php if($pickUpData->post_count-1 != $counter) : ?> <section> <?php endif; ?> <?php elseif($pickUpData->post_count-1 == $counter) : ?> </section> <?php endif; ?> <?php $counter++; endwhile; ?> <?php else : ?> <p>記事がありません。</p> <?php endif; ?>

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

wp-content/themes/sample_theme/index.php ファイルの3行目のエラーのため、PHP コードの変更をロールバックしました。修正し、もう一度保存してください。

Uncaught Error: Call to a member function have_posts() on null in wp-content/themes/sample_theme/index.php:3
Stack trace:
wp-includes/template-loader.php(106): include()
wp-blog-header.php(19): require_once('/home/htemhpvr/...')
index.php(17): require('/home/htemhpvr/...')
{main}
thrown

前提・実現したいこと

文法のエラー原因が見つからない

ヘディングのテキスト

<?php $counter = 0; while($pickUpData->have_posts()): $pickUpData->the_post(); ?>

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

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

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

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

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

tabuu

2021/08/24 00:02

$pickUpData という変数はどこで定義されていますか?
homepage-site

2021/08/30 10:44

<?php $i++; ?> <?php if($i >= 3) break;?> <?php endwhile; ?> <?php endif; ?> <?php rewind_posts(); ?> でいったん投稿の出力を止めて、また再出力する方法にシフトしたところうまくいきました。
guest

回答1

0

自己解決

<?php /* Template Name: キタサンブラック Template Post Type: page */ ?> <?php get_header(); ?> <!--ループ1--> <?php if ( have_posts() ): ?> <?php $i = 0; ?> <?php while ( have_posts() ) : 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></li> <!--リンククラスつきのタイトルを追加--> <div class="img-wrap2"> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></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> <!--//ループ1--> <?php $i++; ?> <?php if($i >= 3) break;?> <?php endwhile; ?> <?php endif; ?> <?php rewind_posts(); ?> <!--RSS記事追加--> <?php include_once( ABSPATH . WPINC . '/feed.php' ); $rss = fetch_feed(' ' ); // Feed URLを入れる if ( !is_wp_error( $rss ) ) { $maxitems = $rss->get_item_quantity( 2 ); $rss_items = $rss->get_items( 0, $maxitems ); } ?> <div class="grid-x grid-margin-y"> <?php if ( !empty( $maxitems ) ) : ?> <?php if ($maxitems == 0) echo '<div class="cell large-14">データがありませんでした.</div>'; else foreach ( $rss_items as $item ) : $first_img = ''; if ( preg_match( '/<img.+?src=[\'"]([^\'"]+?)[\'"].*?>/msi', $item->get_content(), $matches )) $first_img = $matches[1]; if(empty($first_img)) $first_img = "wp-content/uploads/2021/06/ダウンロード-2.png"; // 画像準備完了 ?> <span class="date2"> <?= $item->get_date('Y.m.d') ?> </span> <div class="cell medium-7"> <a href="<?= $item->get_permalink() ?>"> <span class="thumbnail2"> <img class="heit6" src="<?= esc_attr( $first_img ) ?>" alt="" /> </span> </a> <div class="cell medium-79"> <a class="name21" href="<?= $item->get_permalink() ?>"> <?= $item->get_title();// タイトル ?> </a> </div></div> <?php endforeach; endif; ?> </div> <!--//RSS記事終わり--> <!--ループ2--> <?php if ( have_posts() ): ?> <?php $i = 0; ?> <?php while ( have_posts() ) : 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></li> <!--リンククラスつきのタイトルを追加--> <div class="img-wrap2"> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></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> <!--//ループ2--> <?php $i++; ?> <?php if($i >= 3) break;?> <?php endwhile; ?> <?php endif; ?> <?php rewind_posts(); ?> <!--RSS記事追加--> <?php include_once( ABSPATH . WPINC . '/feed.php' ); $rss = fetch_feed(' ' ); // Feed URLを入れる if ( !is_wp_error( $rss ) ) { $maxitems = $rss->get_item_quantity( 2 ); $rss_items = $rss->get_items( 0, $maxitems ); } ?> <div class="grid-x grid-margin-y"> <?php if ( !empty( $maxitems ) ) : ?> <?php if ($maxitems == 0) echo '<div class="cell large-14">データがありませんでした.</div>'; else foreach ( $rss_items as $item ) : $first_img = ''; if ( preg_match( '/<img.+?src=[\'"]([^\'"]+?)[\'"].*?>/msi', $item->get_content(), $matches )) $first_img = $matches[1]; if(empty($first_img)) $first_img = "wp-content/uploads/2021/06/ダウンロード-2.png"; // 画像準備完了 ?> <span class="date2"> <?= $item->get_date('Y.m.d') ?> </span> <div class="cell medium-7"> <a href="<?= $item->get_permalink() ?>"> <span class="thumbnail2"> <img class="heit6" src="<?= esc_attr( $first_img ) ?>" alt="" /> </span> </a> <div class="cell medium-79"> <a class="name21" href="<?= $item->get_permalink() ?>"> <?= $item->get_title();// タイトル ?> </a> </div></div> <?php endforeach; endif; ?> </div> <!--//RSS記事終わり--> <!--ループ3--> <?php if ( have_posts() ): ?> <?php $i = 0; ?> <?php while ( have_posts() ) : 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></li> <!--リンククラスつきのタイトルを追加--> <div class="img-wrap2"> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></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> <!--//ループ3--> <?php $i++; ?> <?php if($i >= 3) break;?> <?php endwhile; ?> <?php rewind_posts(); ?> <!--ページ送り--> <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_sidebar(); ?> <?php get_footer(); ?>

投稿2021/08/30 10:41

homepage-site

総合スコア28

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問