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

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

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

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

ループ

ループとは、プログラミングにおいて、条件に合致している間、複数回繰り返し実行される箇所や、その制御構造を指します

Q&A

解決済

1回答

1796閲覧

WordPressのループの解釈とループ内使用関数について

Akira_papa

総合スコア3

WordPress

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

ループ

ループとは、プログラミングにおいて、条件に合致している間、複数回繰り返し実行される箇所や、その制御構造を指します

0グッド

0クリップ

投稿2021/05/02 22:24

編集2021/05/02 23:08

ある書籍に沿ってWordPressテーマを作成しています。
その中で以下のコードがあり、the_titleやthe_excerptが使われています。
これらの関数はループ内で使用しないといけないと解釈していたのですが、以下のコードにはループが使われていないと思います。

whileやforeachを使った記述がループかと思っていたのですが、解釈が違うのかとも思い調べてみてもわかりませんでした。

画面表示は特に問題はない状態です。

以下のコードはループが使用されていることになるのでしょうか?
もし万が一ループ外だとしたら、それでもtitle等が正しく表示されるのはなぜでしょうか?

教えていただけるとありがたいです。

よろしくお願いいたします。

以下該当部分です <section class="section-contents" id="company"> <div class="wrapper"> <?php $company_page = get_page_by_path('company'); $post = $company_page; setup_postdata($post); ;?> <span class="section-title-en">test</span> <h2 class="section-title"><?php the_title();?></h2> <p class="section-lead"> <?php the_excerpt();?> </p> <div class="section-buttons"> <button type="button" class="button button-ghost" onclick="javascript:location.href = '<?php echo esc_url(home_url('company'));?>';"> <?php the_title();?>を見る </button> </div> <?php wp_reset_postdata();?> </div> </section>

上記はfront-page.phpの一部です。全文は以下の通りです。

<?php get_header(); ?> <section class="section-contents" id="shop"> <div class="wrapper"> <?php $shop_obj = get_page_by_path('shop'); $post = $shop_obj; setup_postdata($post); $shop_title = get_the_title(); ?> <span class="section-title-en">Shop Information</span> <h2 class="section-title"><?php the_title(); ?></h2> <p class="section-lead"><?php echo get_the_excerpt(); ?></p> <?php wp_reset_postdata(); ?> <ul class="shops"> <?php $shop_pages = get_child_pages(4, $shop_obj->ID); if ($shop_pages->have_posts()) : while ($shop_pages->have_posts()) : $shop_pages->the_post(); ?> <li class="shops-item"> <a class="shop-link" href="<?php the_permalink(); ?>"> <div class="shop-image"> <?php the_post_thumbnail('common'); ?> </div> <div class="shop-body"> <p class="name"><?php the_title(); ?></p> <p class="location"></p> <div class="buttonBox"> <button type="button" class="seeDetail">MORE</button> </div> </div> </a> </li> <?php endwhile; endif; wp_reset_postdata(); ?> </ul> <div class="section-buttons"> <button type="button" class="button button-ghost" onclick="javascript:location.href = '<?php echo esc_url(home_url('shop')); ?>';"> <?php echo $shop_title; ?>一覧を見る </button> </div> </div> </section> <section class="section-contents" id="contribution"> <div class="wrapper"> <?php $contribution_obj = get_page_by_path('contribution'); $post = $contribution_obj; setup_postdata($post); $contribution_title = get_the_title(); ?> <span class="section-title-en">Regional Contribution</span> <h2 class="section-title"><?php the_title(); ?></h2> <p class="section-lead"><?php echo get_the_excerpt(); ?></p> <?php wp_reset_postdata(); ?> <div class="articles"> <?php $contribution_pages = get_child_pages(3, $contribution_obj->ID); if ($contribution_pages->have_posts()) : while ($contribution_pages->have_posts()) : $contribution_pages->the_post();; ?> <article class="article-card"> <a class="card-link" href="<?php the_permalink(); ?>"> <div class="card-inner"> <div class="card-image"> <?php the_post_thumbnail('front-contribution'); ?> </div> <div class="card-body"> <p class="title"><?php the_title(); ?></p> <p class="excerpt"><?php echo get_the_excerpt(); ?></p> <div class="buttonBox"> <button type="button" class="seeDetail">MORE</button> </div> </div> </div> </a> </article> <?php endwhile; endif; wp_reset_postdata();?> </div> <div class="section-buttons"> <button type="button" class="button button-ghost" onclick="javascript:location.href = '<?php echo esc_url(home_url('contribution')); ?>';"> <?php echo $contribution_title;?>一覧を見る </button> </div> </div> </section> <section class="section-contents" id="news"> <div class="wrapper"> <?php $term_obj = get_term_by('slug', 'news', 'category'); ?> <span class="section-title-en">News Release</span> <h2 class="section-title"><?php echo $term_obj->name; ?></h2> <p class="section-lead"><?php echo $term_obj->description; ?></p> <ul class="news"> <?php $news_posts = get_specific_posts('post', 'category', 'news', 3); // var_dump($news_posts); if ($news_posts->have_posts()) : while ($news_posts->have_posts()) : $news_posts->the_post(); ?> <li class="news-item"> <a class="detail-link" href="<?php the_permalink(); ?>"> <time class="time"><?php the_time('Y.m.d'); ?></time> <p class="title"><?php the_title(); ?></p> <p class="news-text"><?php the_excerpt(); ?></p> </a> </li> <?php endwhile; endif; wp_reset_postdata(); ?> </ul> <div class="section-buttons"> <button type="button" class="button button-ghost" onclick="javascript:location.href = '<?php echo esc_url(get_term_link($term_obj)); ?>';"> <?php echo $term_obj->name; ?>一覧を見る </button> </div> </div> </section> <section class="section-contents" id="company"> <div class="wrapper"> <?php $company_page = get_page_by_path('company'); $post = $company_page; setup_postdata($post); ;?> <span class="section-title-en">test</span> <h2 class="section-title"><?php the_title();?></h2> <p class="section-lead"> <?php the_excerpt();?> </p> <div class="section-buttons"> <button type="button" class="button button-ghost" onclick="javascript:location.href = '<?php echo esc_url(home_url('company'));?>';"> <?php the_title();?>を見る </button> </div> <?php wp_reset_postdata();?> </div> </section> <?php get_footer(); ?>

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

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

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

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

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

maisumakun

2021/05/02 23:00

これ自体はどこから呼び出されていますか?
Akira_papa

2021/05/02 23:11 編集

front-page.phpを使っており、全文を追加で掲載しました。 別な箇所から呼び出して使っているわけではないです。 質問をご覧いただきありがとうございます。
guest

回答1

0

ベストアンサー

the_**系の関数は大体がグローバル変数$postから値を取得していて
このグローバル変数$postはwp-blog-header.phpwp();処理の中で
WP::register_globalsが実行されセットされます

投稿2021/05/03 16:20

KazuhiroHatano

総合スコア7804

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

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

Akira_papa

2021/05/03 20:19

回答ありがとうございます。 では、今回の件についてはループ外で使われているということになりますか? 表示は問題ないとしても、codexでの記載に反してループ外で使う使い方は、WordPressでサイト制作をする上ではよくあるものだったりするのでしょうか。
KazuhiroHatano

2021/05/04 01:37

まあ一般的なのかどうかはわからないですがちょくちょく見ます テンプレート読み込みより前にグローバル変数$postがセットされる という動作は変わることがないでしょうし テンプレート選択でsingle.phpやsingular.phpなどが選択されたということは 一つの投稿が見つかったということは確定してるわけなので if(have_posts())とかする意味もないっちゃないです ただ個別ページのページ分割機能などはthe_post()で setup_postdata()がされていないと動かないはずなので この辺りの機能を使うなら個別ページでもループをちゃんと書かないとダメです
Akira_papa

2021/05/06 21:43

ご回答ありがとうございました。 勉強になりました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問