tkshrt
@tkshrt

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

Xサーバーでsingle.phpが読み込まれない理由を教えてください

解決したいこと

xサーバーにWordPressの投稿を表示させたいです

public_html/wp/wp-content/themes/twentytwentythree/single.php
上記の階層に下記内容のsingle.phpを入れていますが
投稿を開くとURLは正しいのにpublic_html/index.php
のindex.phpの内容が表示されてしまいます

トップページでは下記内容のindex.phpを読み込み最新投稿が3つ表示されるようにはなっているので、WordPressの投稿を読み込めていない訳ではなさそうです。
しかし、投稿を表示しようとするとURLは正しく表示されていますが、index.phpの内容が表示されてしまい、single.phpが読み込めていません。

single.phpを読み込まない理由を教えていただきたいです。

single.php

<?php
get_header(); // ヘッダーを読み込み
?>

<main>
    <section class="single-post">
        <div class="container">
            <?php
            if (have_posts()) :
                while (have_posts()) : the_post();
            ?>
                    <article>
                        <h1><?php the_title(); ?></h1>
                        <p class="post-date"><?php echo get_the_date('Y年n月j日'); ?></p>
                        <div class="post-thumbnail">
                            <?php
                            if (has_post_thumbnail()) {
                                the_post_thumbnail('large');
                            }
                            ?>
                        </div>
                        <div class="post-content">
                            <?php the_content(); ?>
                        </div>
                    </article>
            <?php
                endwhile;
            else :
                echo '<p>投稿が見つかりません。</p>';
            endif;
            ?>
        </div>
    </section>
</main>

<?php
get_footer(); // フッターを読み込み
?>

index.php

<section class="main-news">
	<div class="main-news-container">
		<h3 class="vartical news-ttl fadeUpTrigger fadeUp">お知らせ</h3>
		<ul class="main-news-list fadeUp">
			<?php
			$recent_posts = new WP_Query([
				'posts_per_page' => 3, // 投稿数を3に制限
				'post_status'    => 'publish',
			]);

			if ($recent_posts->have_posts()):
				while ($recent_posts->have_posts()): $recent_posts->the_post();
			?>
				<li class="main-news-item">
					<a href="<?php the_permalink(); ?>">
						<!-- アイキャッチ画像 or デフォルト画像 -->
						<div class="main-news-thumbnail">
							<?php if (has_post_thumbnail()): ?>
								<?php the_post_thumbnail('medium'); ?>
							<?php else: ?>
								<img src="sample.jpg" alt="サンプル画像">
							<?php endif; ?>
						</div>

						<!-- タイトル -->
						<h4 class="main-news-title"><?php the_title(); ?></h4>

						<!-- 本文抜粋 -->
						<p class="main-news-excerpt"><?php echo wp_trim_words(get_the_excerpt(), 100, '...'); ?></p>
					</a>
				</li>
			<?php
				endwhile;
				wp_reset_postdata();
			endif;
			?>
		</ul>
		<!-- 「お知らせ一覧はこちら」ボタン -->
		<div class="main-news-button-container fadeUp">
			<a href="https://www.matsukou.hyogo.jp/news" class="main-news-button">お知らせ一覧はこちら</a>
		</div>
	</div>
</section>
0

1Answer

投稿を開くとURLは正しいのに

投稿を開くときのURLを書かないと正しいかどうかわかりません

http:/localhost/

で public_html/index.php が開くのなら

public_html/wp/wp-content/themes/twentytwentythree/single.php

を開くときのURLは

http:/localhost/wp/wp-content/themes/twentytwentythree/single.php

こうかな?
これが開かないと言うこと?

0Like

Comments

  1. @tkshrt

    Questioner

    https://sample.com
    

    の投稿である

    https://sample.com/toukou
    

    を開くと
    投稿の内容がsingle.phpによって表示される認識なのですが、
    表示されているのがトップページのindex.phpの内容になってしまいます。

  2. サーバーの設定がわからないけど標準の設定なら

    https://sample.com/toukou
    

    で呼ばれるのは

    public_html/toukou.php
    

    じゃないの?
    最後がスラッシュなら

    public_html/toukou/index.php
    

    かもしれないけど

  3. Twenty Twenty-Two以降のデフォルトテーマはphpファイルではなく、外観→エディタ→テンプレートからテンプレートを修正する方式のはずです。
    phpファイルでテンプレートを作っていく方式でテーマを編集するのであれば、underscoresのようなスターターテーマを利用するほうがよいかと思います。

    また、Query MonitorやShow Current Templateなどのプラグインを入れれば自分がいまいるページでどのテンプレートが呼ばれているかわかりますので、いずれか導入されるとよいでしょう。

Your answer might help someone💌