4
3

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 3 years have passed since last update.

WordPressの個別記事をページ遷移ではなく、モーダル表示にする方法

Posted at

※前提として、カスタム投稿を表示させる場合のコードになります。

①Remodalをダウンロード
http://vodkabears.github.io/remodal/
読み込むファイルは「remodal.min.js」「remodal.css」「remodal-default-theme.css」の3つ。
※パスは任意で変更してください。

<link rel="stylesheet" href="remodal.css">
<link rel="stylesheet" href="remodal-default-theme.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="remodal.min.js"></script>

②投稿部分を作成
ポイントとしてはaタグのリンク先を記事のIDに設定すること。
モーダルのdata-remodal-idと揃えることで、紐付けが完了する。
※表示させたい内容は任意で変更してください。

<?php
$args = [
'post_type' => 'works', // カスタム投稿名が「works」の場合
'posts_per_page' => 5, // 表示させる数
];
$my_query = new WP_Query($args); ?>
<?php if ($my_query->have_posts()): ?>

<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<article class="">
<a href="#<?php the_ID(); ?>">
<figure class=""><img class="" src="" alt=""></figure>
<h3 class="mod-slide__title"></h3>
<p class=""><?php the_title(); ?></p>
</a>
</article>
<?php endwhile; ?>
<?php else: ?>
<?php endif; wp_reset_postdata(); ?>

③モーダル部分を作成
先程記載した通り、data-remodal-idに記事のIDを設定してください。
また、モーダルの親要素にclass="remodal"を付与してください。
こちらで完了です。

<?php
$args = [
'post_type' => 'works',
'posts_per_page' => 5,
];
$my_query = new WP_Query($args); ?>  
<?php if ($my_query->have_posts()): ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<article class="remodal" data-remodal-id="<?php the_ID(); ?>">
<button data-remodal-action="close" class="remodal-close"></button>
<figure><img src="" alt=""></figure>
<h3 class=""></h3>
<p class=""></p>
</article>
<?php endwhile; ?>
<?php else: ?>
<?php endif; wp_reset_postdata(); ?>
4
3
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
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?