ワードプレスのオリジナルテーマを作っています。
トップページに「サムネイル付きのカテゴリー一覧」を作り、各カテゴリーに属する投稿を2件ずつ表示したいと思っています。
カテゴリーは追加されたものが自動的に更新されるようにしたいです。
現状のコードは下記の通りです。投稿時の画像ではなく無関係な画像を持ってきてしまいます。
<div class="text-kategori"> <div id="a"> <?php $categories = get_categories(); foreach($categories as $category) : ?> <h2> <a href="<?php echo get_category_link( $category->term_id ); ?>"><?php echo $category->cat_name; ?>の新着記事</a></h2> <section class="mt-4"> <div class="row"> <div class="col-5"> <a href="<?php the_permalink() ?>"> <?php $thumb = get_the_post_thumbnail(); echo $thumb; ?> <?php if (has_post_thumbnail()) : ?> <?php else: ?> <img src="<?php echo get_template_directory_uri(); ?>/imgs/img-new.png" alt="" width="100%" height="auto" class="mb-3"> <?php endif; ?> </a> </div> <div class="col-7"> <?php query_posts('cat='.$category->cat_ID); if (have_posts()) : while (have_posts()) : the_post(); ?> <a href="<?php the_permalink() ?>"> <h3> <?php the_title(); ?> </h3> <p class="article-text"><?php echo mb_substr( get_the_excerpt(), 0, 30 ) . '[...]'; ?></p> </a> <?php endwhile; endif; ?> </div> <?php endforeach; ?> </div> <!--row--> </section> </div> <!--div id="a"--> </div> <!--text-kategori--> </div> <!--content--> コード
①投稿時の画像がセットされるようにしたい。
②投稿した記事は2件ずつ表示したい。
いろいろなサイトを参考にしてやってみましたが、うまくいきません。
php知識乏しくてすみません。
よろしくご教授ください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。