0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Wordpressのブログ記事を、特定のタグが設定されている記事だけ4件取得して表示したい

Posted at

表示させたい固定ページのphpファイルを開いて編集

以下のコードをタグ名、取得件数や表示させるときのHTML構造を変更して貼り付けるだけです。

<?php
    $tag = '取得したいタグ名'; // タグのスラッグを指定
    $args = array(
        'post_type' => 'post', // 投稿タイプを指定
        'tag' => $tag, // タグを指定
        'posts_per_page' => 4, // 取得する記事数を指定
    );

    $query = new WP_Query($args);

    if ($query->have_posts()) :
        while ($query->have_posts()) : $query->the_post();
            // ここに記事を表示するためのコードを追加
            ?>
            <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                <header class="entry-header">
                    <h2 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                </header>
                <div class="entry-content">
                    <?php the_excerpt(); ?>
                </div>
            </article>
            <?php
        endwhile;
        wp_reset_postdata();
    else :
        // 記事が見つからない場合の表示
        echo '該当する記事はありません。';
    endif;
?>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?