WordPressにはショートコードと呼ばれる短いコードを利用することで、簡単に特定の機能を追加できる機能があります。この記事では、WordPressのショートコードを使って、MasonryとSwiperのライブラリを実装する方法について説明します。

 

まずは、Masonryの実装についてです。Masonryは、縦横に配置されたグリッド状のレイアウトを簡単に作成できるライブラリです。WordPressのショートコードを使って、簡単にMasonryのレイアウトを実装することができます。

 

次に、Swiperの実装についてです。Swiperは、スライダーを実装することができるライブラリです。WordPressのデフォルトのギャラリー機能を使用する場合、Swiperをうまく組み込むことができませんでした。

 

しかし、Swiperを使用したい場合は、独自のカスタムコードを作成することで、WordPressのギャラリー機能をSwiperと組み合わせることができます。

 


function swpViewModalPromiseNH1A() {
//Swiper:swiper-viewModal:JS
return new Promise(function (resolve, reject) {
  setTimeout(function () {
  //最初の処理:「.masonry>img」に、連番で「data-slito=""」を付与
  let lstMsryHtmbNH0As = document.querySelectorAll('.masonry-href-thumbnail>img');
  let msryHtmbNH0As = Array.prototype.slice.call(lstMsryHtmbNH0As); //Array.NodeList
  msryHtmbNH0As.forEach((msryHtmbNH0A, index) => {
    msryHtmbNH0A.setAttribute('data-sldto', index + 1);
  });
  resolve();
  }, 0);
})
.then(function () {
return new Promise(function (resolve, reject) {
  setTimeout(function () {
    //2番目の処理:Masonry.js
    let lstMsryWrpNH0As = document.querySelectorAll(".masonry-container .masonry-wrapper");
    let msryWrpNH0As = Array.prototype.slice.call(lstMsryWrpNH0As); //Array.NodeList

    //ウィンドウリサイズ・画像ロード時のイベント
    let timerNH0A = 0;
    let phancMsryRsizImeloadNH0A = function () {
      if (timerNH0A) clearTimeout(timerNH0A);
      timerNH0A = setTimeout(() => {
        //Masonry.js
        $(msryWrpNH0As).masonry({
          itemSelector: '.masonry-wrapper .masonry-slide',
          fitWidth: true //親要素の幅をグリッド幅に合わせて設定
        });
      }, 300);
    };
    //ウィンドウリサイズ時のイベント呼び出し登録
    window.addEventListener('resize', function (e) {
      phancMsryRsizImeloadNH0A();
    });
    //画像ロード時のイベント呼び出し登録(imagesLoaded)
    imagesLoaded(msryWrpNH0As, function (e) {
      phancMsryRsizImeloadNH0A();
    }); ///window.'resize
    resolve();
  }, 0);
})
})
.then(function () {
return new Promise(function (resolve, reject) {
  setTimeout(function () {
    //3番目の処理:Swiperモーダル設定
    let lstMdlClsrNH1As = document.querySelectorAll(".js-modalCloser");
    let mdlClsrNH1As = Array.prototype.slice.call(lstMdlClsrNH1As);
    let lstMdlOpnrNH1As = document.querySelectorAll(".js-modalOpener");
    let mdlOpnrNH1As = Array.prototype.slice.call(lstMdlOpnrNH1As); //Array.NodeList
    let lstTriggerNH1As = document.querySelectorAll(".swiper-container.swiper-viewModal");
    let triggerNH1As = Array.prototype.slice.call(lstTriggerNH1As);

    //トリガーからIDを取り出して、ループ処理
    let lngtNH1A = triggerNH1As.length;
    for (var n = 0; n < lngtNH1A; n++) {
      var targetNH1A = triggerNH1As[n].getAttribute('id');
      var targetNH1A = document.getElementById(targetNH1A);
      //action(swpViewModal)
      let swpViewModalNH1A = new Swiper(targetNH1A, {
        //effect: 'fade', //スライドエフェクト
        slidesPerView: 1, //コンテナ内に表示させるスライド数
        spaceBetween: 0, //スライド間の余白(px)
        //direction: 'vertical', //縦方向
        centeredSlides: true, //中央配置
        loop: true, //ループ
        //loopedSlides: 9, //スライドの枚数と同じ値を指定
        speed: 1000, //スライドアニメーションの速度(ミリ秒)
        //autoplay: { //自動再生
        //  delay: 3000, //次のスライドへの切替時間(ミリ秒)
        //  disableOnInteraction: true, //ユーザー操作で自動再生ストップ
        //  stopOnLastSlide: true //最終スライドで自動再生ストップ
        //},
        grabCursor: true, //PCカーソルを「掴む」マークにする
        //allowTouchMove: true, //スワイプ無効

        pagination: {
          el: '.swiper-pagination', //ページネーション要素のクラス
          clickable: true, //クリック有効
          type: 'bullets'
        },
        navigation: {
          nextEl: '.swiper-button-next', //「次へ」ボタン
          prevEl: '.swiper-button-prev', //「前へ」ボタン
        },
        //on: { //イベント },
      });

      //モーダルを表示するボタンをクリックしたとき
      let phancMdlOpnrNH1A = function () {
        let lngtNH1A = mdlOpnrNH1As.length;
        for (let n = 0; n < lngtNH1A; n++) {
          mdlOpnrNH1As[n].addEventListener('click', function (e) {
            e.preventDefault(); // デフォルトの動作をキャンセル
            //imgのdata属性の値を取得して、数値に変換する
            let openImgNH1A = mdlOpnrNH1As[n].querySelector('img');
            let dataSldTo = openImgNH1A.dataset.sldto;
            //let dataSldTo = openImgNH1A.getAttribute('title');
            let intSldTo = parseInt(dataSldTo);
            if (isNaN(intSldTo)) {
              intSldTo = 1;
            };
            swpViewModalNH1A.slideTo(intSldTo);
            targetNH1A.classList.add("is-active");
          });
        };
      }(); ///.for.addEventListener('click'

      //モーダルを閉じるボタンをクリックしたとき
      let phancMdlClsrNH1A = function () {
        mdlClsrNH1As.forEach((mdlClsrNH1A) => {
          mdlClsrNH1A.addEventListener("click", () => {
            targetNH1A.classList.remove("is-active");
          });
        });
      }(); ///.forEach.addEventListener('click'
    }; //for{}
    resolve();
  }, 0);
})
})
.catch(function (error) {
console.log(error);
});
}
swpViewModalPromiseNH1A();    

 


/* masonry-viewModalTmb
-------------------------------------- */
.masonry-container.masonry-viewModalTmb {
  position: relative;
  /* Flex */
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: flex-start;
  width: 1050px;
  max-width: 100%;
  margin-left: auto;
  margin-right: auto;
  overflow: hidden;
  margin-bottom: 3rem;
}

.masonry-viewModalTmb .masonry-wrapper {
  width: 100% !important;
}

.masonry-viewModalTmb .masonry-wrapper::after {
  content: "";
  display: block;
  clear: both;
}

.masonry-viewModalTmb .masonry-wrapper>li {
  width: 100%;
  max-width: 90vw;
  /* padding: 上  横  下 */
  padding: 10px 10px 10px;
}

/* (PC) */
@media print,
screen and (min-width: 768px) {
  .masonry-viewModalTmb .masonry-wrapper>li {
    width: 33.3%;
  }
}

.masonry-viewModalTmb .masonry-ifield {
  padding: 0;
  box-shadow: rgba(0, 0, 0, 0.12) 0px 1px 3px,
    rgba(0, 0, 0, 0.24) 0px 1px 2px;
  overflow: hidden;
}

.masonry-viewModalTmb .masonry-href-thumbnail {
  position: relative;
  display: block;
  width: 100%;
  height: auto;
  overflow: hidden;
  margin-bottom: 0;
}

.masonry-viewModalTmb .masonry-href-thumbnail>img {
  width: 100%;
  height: auto;
  pointer-events: auto;
}

.masonry-viewModalTmb .masonry-caption {
  display: none;
}




/* swiper-viewModal
-------------------------------------- */
/* モーダル背景のオーバーレイ部分 */
.swiper-modalOuter:has(+.swiper-viewModal.is-active) {
  position: fixed;
  z-index: 100;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
  width: 100%;
  height: 100vh;
  background-color: rgba(120, 123, 131, 0.8);
  cursor: pointer;
}

.swiper-container.swiper-viewModal {
  position: fixed;
  z-index: 110;
  /* ヘッダー分調整 */
  top: 4.5rem;
  bottom: 0px;
  left: 0;
  right: 0;
  margin: auto;
  background: #FFF;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 1050px;
  max-width: 100%;
  /* ヘッダー分調整 */
  height: calc(100% - 10.5rem);
  /* Pagenation分調整 */
  padding: 0px 0px 30px;
  transition: opacity 0.5s ease-in;
  opacity: 0;
  pointer-events: none;
  overflow: hidden;
}

/* モーダルがactiveの時 */
.swiper-container.swiper-viewModal.is-active {
  opacity: 1;
  pointer-events: auto;
}

/* モーダルを閉じるボタン */
.swiper-modalCloser {
  cursor: pointer;
  position: absolute;
  z-index: 20;
  right: 0;
  top: 0;
  width: 44px;
  height: 44px;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  padding-bottom: 2px;
  background: #fff;
  border: 1px solid #ccc;
}

.swiper-viewModal .swiper-wrapper {
  /* navigation分の余白調整 */
  left: 0px;
}

.swiper-viewModal .swiper-wrapper>li {
  height: auto;
  max-height: 80hvh;
  max-height: 80vh;
  text-align: center;
  /* padding: 上  横  下 */
  padding: 2rem 2rem 0px;
}

.swiper-viewModal .swiper-ifield {
  max-height: 100%;
  padding: 0;
  box-shadow: rgba(0, 0, 0, 0.12) 0px 1px 3px,
    rgba(0, 0, 0, 0.24) 0px 1px 2px;
  overflow: hidden;
}

.swiper-viewModal .swiper-href-thumbnail {
  position: relative;
  display: block;
  width: 100%;
  height: auto;
  overflow: hidden;
  margin-bottom: 0;
}

.swiper-viewModal .swiper-href-thumbnail>img {
  position: static;
  top: 0;
  height: calc(80vh - 30px);
  height: calc(80dvh - 30px);
  width: auto;
  max-width: 100% !important;
  margin-top: 0 !important;
  margin-bottom: 0 !important;
  border: none !important;
  object-fit: cover;
}

.swiper-viewModal .swiper-caption {
  position: absolute;
  top: 11%;
  left: 2rem;
  right: auto;
  margin: auto;
  width: auto;
  height: auto;
  /* 文字数次第で調整 */
  padding: 0em 2.3em 0em 1.3em;
  margin-bottom: 0 !important;
  text-align: left;
  /* Flex */
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  /* .cap-w-img */
  text-shadow: none;
  background: hsla(0, 0%, 96%, 0.5);
  transform: rotateZ(-16deg);
}

.swiper-viewModal .swiper-caption * {
  color: #666;
}

.swiper-viewModal .swiper-caption .swiper-title {
  font-family: 'Josefin Sans', sans-serif;
  font-size: 45px;
  font-size: clamp(25.9px, calc(25.9px + ((1vw - 3.6px) * 2.98438)), 45px);
  min-height: 0vw;
  color: hsla(0, 0%, 44%, 1);
  /* Flex: order */
  order: 1;
}

.swiper-viewModal .swiper-caption .swiper-title,
.swiper-viewModal .swiper-caption .swiper-title a {
  font-weight: 600;
  border-bottom: none;
  color: var(--c-magique_p) !important;
}

.swiper-viewModal .swiper-caption .swiper-meta-fields {
  /* Flex: order */
  order: 2;
  font-size: 13px;
  color: var(--c-magique_p);
  /* 区切りのカンマ・スラッシュを非表示にする */
  font-size: 0px;
  color: transparent;
}

/* 日付 */
.swiper-viewModal .swiper-meta-fields .entry-date {
  display: inline-block;
  vertical-align: middle;
  line-height: 1;
  font-size: 13px;
  padding-bottom: 0.1em;
  margin-right: 4px;
}

/* タグ */
.swiper-viewModal .swiper-meta-fields .terms a[class^="swiper-tax-"] {
  display: inline-block;
  vertical-align: middle;
  padding: 0.2em 0.4em 0.3em;
  border-radius: 2px;
  line-height: 1;
  font-size: 11px;
  text-align: center;
  background: hsla(0, 0%, 96%, 1);
  pointer-events: none;
  margin-right: 4px;
}

/* カテゴリー(カテゴリー名を「cat-***」にしておく) */
.swiper-viewModal .swiper-meta-fields .terms a[class^="swiper-tax-cat-"] {
  position: absolute;
  top: 0px;
  left: 0px;
  margin: auto;
  display: inline-block;
  width: auto;
  height: auto;
  padding: 0.6em 1em 0.6em;
  line-height: 1;
  font-size: 13px;
  text-align: center;
  font-weight: 600;
  background: var(--bgc-magique_accent);
  color: #FFF;
  border-radius: 0px;
  /* カテゴリだけリンクにする */
  pointer-events: auto;
}

.swiper-viewModal .swiper-caption .swiper-content {
  /* Flex: order */
  order: 3;
  margin-top: 0.4em;
  width: 100%;
  font-size: var(--va-saraciere-fz_smaller);
  color: var(--c-magique_p);
}

.swiper-viewModal .swiper-content .btn {
  margin-top: 15px;
  display: block;
  width: 250px;
  max-width: 90%;
  margin-left: auto;
  margin-right: auto;
  padding: 6px 12px;
  font-size: 14px;
  line-height: 1.44;
  font-weight: 400;
  text-align: center;
  white-space: nowrap;
  border-radius: 4px;
}

.swiper-viewModal .swiper-readmore.btn {
  background: var(--bgc-magique_base);
  color: #428bca;
  border-radius: 0px;
  border: 1px solid var(--bgc-magique_accent);
}

.swiper-viewModal a:hover.swiper-readmore.btn {
  background: #428bca;
  color: #FFF;
}

/* navigation */
.swiper-viewModal .swiper-navigation {
  position: absolute;
  z-index: 10;
  top: 0;
  bottom: 0;
  left: 0px;
  right: 15px;
  margin: auto;
  width: 100%;
  max-width: 90vw;
  height: auto;
}

.swiper-viewModal .swiper-button-prev,
.swiper-viewModal .swiper-button-next {
  display: inline-flex;
  justify-content: center;
  align-items: center;
  transform: translateZ(0);
  transition: all 0.1s ease-out;
  width: 74px;
  height: 74px;
  font-size: 74px;
  text-align: center;
  text-shadow: 0 1px 2px rgba(0, 0, 0, .6);
  border-radius: 999px;
  opacity: 1;
  background: transparent;
  color: hsla(0, 0%, 44%, 1);
}

.swiper-viewModal .swiper-button-prev {
  position: absolute;
  top: 0px;
  bottom: 0px;
  left: -12px;
  margin: auto;
}

.swiper-viewModal .swiper-button-next {
  position: absolute;
  top: 0px;
  bottom: 0px;
  right: -12px;
  margin: auto;
}

.swiper-viewModal .swiper-button-prev::before,
.swiper-viewModal .swiper-button-next::before {
  font-family: 'Material Symbols Sharp';
  font-variation-settings:
    'FILL'0,
    'wght'200;
  margin: auto;
  width: 1em;
  height: 1em;
  line-height: 1;
  font-size: 1em;
  text-align: center;
  color: inherit;
}

.swiper-viewModal .swiper-button-prev::before {
  content: "\e5cb";
}

.swiper-viewModal .swiper-button-next::before {
  content: "\e5cc";
}

@media (any-hover: hover) {
  .swiper-viewModal .swiper-button-prev:hover {
    opacity: 1;
    transform: translateX(-4px);
  }

  .swiper-viewModal .swiper-button-next:hover {
    opacity: 1;
    transform: translateX(4px);
  }
}

/* pagination */
.swiper-viewModal .swiper-pagination {
  position: static;
  margin-left: auto;
  margin-right: auto;
  /* navigation分の余白調整 */
  padding: 15px 0px 15px 0px;
}

.swiper-viewModal .swiper-pagination-bullet {
  display: inline-block;
  vertical-align: middle;
  width: 10px;
  height: 10px;
  border-radius: 999px;
  background: #CECECE;
  border: 1px solid #CECECE;
  opacity: 1;
}

.swiper-viewModal .swiper-pagination-bullet-active {
  width: 12px;
  height: 12px;
  background: #428bca;
  opacity: 1;
}

 


 '',
    'view' => '',
    'ids' => ''
  ), $atts));

  $gallery_items = '';
  $ids_array = explode(',', $ids);

  foreach ($ids_array as $id) {
    $gallery_item = wp_get_attachment_image($id, 'full');
    $gallery_items .= '
  • ' . $gallery_item . '
  • '; } $output = ''; //masonry-container $output .= '
    '; $output .= '
      '; $output .= $gallery_items; $output .= '
    '; $output .= '
    '; return $output; } add_shortcode('masonry', 'shortcode_masonry'); '', 'view' => '', 'ids' => '' ), $atts)); $gallery_items = ''; $ids_array = explode(',', $ids); foreach ($ids_array as $id) { $gallery_item = wp_get_attachment_image($id, 'full'); $gallery_items .= '
  • ' . $gallery_item . '
  • '; } $output = ''; //masonry-container $output .= '
    '; $output .= '
      '; $output .= $gallery_items; $output .= '
    '; $output .= '
    '; return $output; } add_shortcode('masonry', 'shortcode_masonry'); //shortcode
    ×
  • <p><iframe height="300" style="width: 100%;" scrolling="no" title="【PHP】swiper-viewModal.php" src="https://codepen.io/sarap422/embed/BaqQMQv?default-tab=result" frameborder="no" loading="lazy" allowtransparency="true" allowfullscreen="allowfullscreen"><span data-mce-type="bookmark" style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;" class="mce_SELRES_start"></span><br /> See the Pen <a href="https://codepen.io/sarap422/pen/BaqQMQv"><br /> 【PHP】swiper-viewModal.php</a> by sarap422 (<a href="https://codepen.io/sarap422">@sarap422</a>)<br /> on <a href="https://codepen.io">CodePen</a>.<br /> </iframe></p> <p> </p> <p>WordPressにはショートコードと呼ばれる短いコードを利用することで、簡単に特定の機能を追加できる機能があります。この記事では、WordPressのショートコードを使って、MasonryとSwiperのライブラリを実装する方法について説明します。</p> <p> </p> <p>まずは、Masonryの実装についてです。Masonryは、縦横に配置されたグリッド状のレイアウトを簡単に作成できるライブラリです。WordPressのショートコードを使って、簡単にMasonryのレイアウトを実装することができます。</p> <p> </p> <p>次に、Swiperの実装についてです。Swiperは、スライダーを実装することができるライブラリです。WordPressのデフォルトのギャラリー機能を使用する場合、Swiperをうまく組み込むことができませんでした。</p> <p> </p> <p>しかし、Swiperを使用したい場合は、独自のカスタムコードを作成することで、WordPressのギャラリー機能をSwiperと組み合わせることができます。</p> <p> </p> <pre class="language-javascript"><code> function swpViewModalPromiseNH1A() { //Swiper:swiper-viewModal:JS return new Promise(function (resolve, reject) { setTimeout(function () { //最初の処理:「.masonry>img」に、連番で「data-slito=""」を付与 let lstMsryHtmbNH0As = document.querySelectorAll('.masonry-href-thumbnail>img'); let msryHtmbNH0As = Array.prototype.slice.call(lstMsryHtmbNH0As); //Array.NodeList msryHtmbNH0As.forEach((msryHtmbNH0A, index) => { msryHtmbNH0A.setAttribute('data-sldto', index + 1); }); resolve(); }, 0); }) .then(function () { return new Promise(function (resolve, reject) { setTimeout(function () { //2番目の処理:Masonry.js let lstMsryWrpNH0As = document.querySelectorAll(".masonry-container .masonry-wrapper"); let msryWrpNH0As = Array.prototype.slice.call(lstMsryWrpNH0As); //Array.NodeList //ウィンドウリサイズ・画像ロード時のイベント let timerNH0A = 0; let phancMsryRsizImeloadNH0A = function () { if (timerNH0A) clearTimeout(timerNH0A); timerNH0A = setTimeout(() => { //Masonry.js $(msryWrpNH0As).masonry({ itemSelector: '.masonry-wrapper .masonry-slide', fitWidth: true //親要素の幅をグリッド幅に合わせて設定 }); }, 300); }; //ウィンドウリサイズ時のイベント呼び出し登録 window.addEventListener('resize', function (e) { phancMsryRsizImeloadNH0A(); }); //画像ロード時のイベント呼び出し登録(imagesLoaded) imagesLoaded(msryWrpNH0As, function (e) { phancMsryRsizImeloadNH0A(); }); ///window.'resize resolve(); }, 0); }) }) .then(function () { return new Promise(function (resolve, reject) { setTimeout(function () { //3番目の処理:Swiperモーダル設定 let lstMdlClsrNH1As = document.querySelectorAll(".js-modalCloser"); let mdlClsrNH1As = Array.prototype.slice.call(lstMdlClsrNH1As); let lstMdlOpnrNH1As = document.querySelectorAll(".js-modalOpener"); let mdlOpnrNH1As = Array.prototype.slice.call(lstMdlOpnrNH1As); //Array.NodeList let lstTriggerNH1As = document.querySelectorAll(".swiper-container.swiper-viewModal"); let triggerNH1As = Array.prototype.slice.call(lstTriggerNH1As); //トリガーからIDを取り出して、ループ処理 let lngtNH1A = triggerNH1As.length; for (var n = 0; n < lngtNH1A; n++) { var targetNH1A = triggerNH1As[n].getAttribute('id'); var targetNH1A = document.getElementById(targetNH1A); //action(swpViewModal) let swpViewModalNH1A = new Swiper(targetNH1A, { //effect: 'fade', //スライドエフェクト slidesPerView: 1, //コンテナ内に表示させるスライド数 spaceBetween: 0, //スライド間の余白(px) //direction: 'vertical', //縦方向 centeredSlides: true, //中央配置 loop: true, //ループ //loopedSlides: 9, //スライドの枚数と同じ値を指定 speed: 1000, //スライドアニメーションの速度(ミリ秒) //autoplay: { //自動再生 // delay: 3000, //次のスライドへの切替時間(ミリ秒) // disableOnInteraction: true, //ユーザー操作で自動再生ストップ // stopOnLastSlide: true //最終スライドで自動再生ストップ //}, grabCursor: true, //PCカーソルを「掴む」マークにする //allowTouchMove: true, //スワイプ無効 pagination: { el: '.swiper-pagination', //ページネーション要素のクラス clickable: true, //クリック有効 type: 'bullets' }, navigation: { nextEl: '.swiper-button-next', //「次へ」ボタン prevEl: '.swiper-button-prev', //「前へ」ボタン }, //on: { //イベント }, }); //モーダルを表示するボタンをクリックしたとき let phancMdlOpnrNH1A = function () { let lngtNH1A = mdlOpnrNH1As.length; for (let n = 0; n < lngtNH1A; n++) { mdlOpnrNH1As[n].addEventListener('click', function (e) { e.preventDefault(); // デフォルトの動作をキャンセル //imgのdata属性の値を取得して、数値に変換する let openImgNH1A = mdlOpnrNH1As[n].querySelector('img'); let dataSldTo = openImgNH1A.dataset.sldto; //let dataSldTo = openImgNH1A.getAttribute('title'); let intSldTo = parseInt(dataSldTo); if (isNaN(intSldTo)) { intSldTo = 1; }; swpViewModalNH1A.slideTo(intSldTo); targetNH1A.classList.add("is-active"); }); }; }(); ///.for.addEventListener('click' //モーダルを閉じるボタンをクリックしたとき let phancMdlClsrNH1A = function () { mdlClsrNH1As.forEach((mdlClsrNH1A) => { mdlClsrNH1A.addEventListener("click", () => { targetNH1A.classList.remove("is-active"); }); }); }(); ///.forEach.addEventListener('click' }; //for{} resolve(); }, 0); }) }) .catch(function (error) { console.log(error); }); } swpViewModalPromiseNH1A(); </code></pre> <p> </p> <pre class="language-css"><code> /* masonry-viewModalTmb -------------------------------------- */ .masonry-container.masonry-viewModalTmb { position: relative; /* Flex */ display: flex; flex-direction: column; justify-content: flex-start; align-items: flex-start; width: 1050px; max-width: 100%; margin-left: auto; margin-right: auto; overflow: hidden; margin-bottom: 3rem; } .masonry-viewModalTmb .masonry-wrapper { width: 100% !important; } .masonry-viewModalTmb .masonry-wrapper::after { content: ""; display: block; clear: both; } .masonry-viewModalTmb .masonry-wrapper>li { width: 100%; max-width: 90vw; /* padding: 上 横 下 */ padding: 10px 10px 10px; } /* (PC) */ @media print, screen and (min-width: 768px) { .masonry-viewModalTmb .masonry-wrapper>li { width: 33.3%; } } .masonry-viewModalTmb .masonry-ifield { padding: 0; box-shadow: rgba(0, 0, 0, 0.12) 0px 1px 3px, rgba(0, 0, 0, 0.24) 0px 1px 2px; overflow: hidden; } .masonry-viewModalTmb .masonry-href-thumbnail { position: relative; display: block; width: 100%; height: auto; overflow: hidden; margin-bottom: 0; } .masonry-viewModalTmb .masonry-href-thumbnail>img { width: 100%; height: auto; pointer-events: auto; } .masonry-viewModalTmb .masonry-caption { display: none; } /* swiper-viewModal -------------------------------------- */ /* モーダル背景のオーバーレイ部分 */ .swiper-modalOuter:has(+.swiper-viewModal.is-active) { position: fixed; z-index: 100; top: 0; bottom: 0; left: 0; right: 0; margin: auto; width: 100%; height: 100vh; background-color: rgba(120, 123, 131, 0.8); cursor: pointer; } .swiper-container.swiper-viewModal { position: fixed; z-index: 110; /* ヘッダー分調整 */ top: 4.5rem; bottom: 0px; left: 0; right: 0; margin: auto; background: #FFF; display: flex; flex-direction: column; justify-content: center; align-items: center; width: 1050px; max-width: 100%; /* ヘッダー分調整 */ height: calc(100% - 10.5rem); /* Pagenation分調整 */ padding: 0px 0px 30px; transition: opacity 0.5s ease-in; opacity: 0; pointer-events: none; overflow: hidden; } /* モーダルがactiveの時 */ .swiper-container.swiper-viewModal.is-active { opacity: 1; pointer-events: auto; } /* モーダルを閉じるボタン */ .swiper-modalCloser { cursor: pointer; position: absolute; z-index: 20; right: 0; top: 0; width: 44px; height: 44px; display: inline-flex; justify-content: center; align-items: center; padding-bottom: 2px; background: #fff; border: 1px solid #ccc; } .swiper-viewModal .swiper-wrapper { /* navigation分の余白調整 */ left: 0px; } .swiper-viewModal .swiper-wrapper>li { height: auto; max-height: 80hvh; max-height: 80vh; text-align: center; /* padding: 上 横 下 */ padding: 2rem 2rem 0px; } .swiper-viewModal .swiper-ifield { max-height: 100%; padding: 0; box-shadow: rgba(0, 0, 0, 0.12) 0px 1px 3px, rgba(0, 0, 0, 0.24) 0px 1px 2px; overflow: hidden; } .swiper-viewModal .swiper-href-thumbnail { position: relative; display: block; width: 100%; height: auto; overflow: hidden; margin-bottom: 0; } .swiper-viewModal .swiper-href-thumbnail>img { position: static; top: 0; height: calc(80vh - 30px); height: calc(80dvh - 30px); width: auto; max-width: 100% !important; margin-top: 0 !important; margin-bottom: 0 !important; border: none !important; object-fit: cover; } .swiper-viewModal .swiper-caption { position: absolute; top: 11%; left: 2rem; right: auto; margin: auto; width: auto; height: auto; /* 文字数次第で調整 */ padding: 0em 2.3em 0em 1.3em; margin-bottom: 0 !important; text-align: left; /* Flex */ display: flex; flex-direction: column; justify-content: flex-start; /* .cap-w-img */ text-shadow: none; background: hsla(0, 0%, 96%, 0.5); transform: rotateZ(-16deg); } .swiper-viewModal .swiper-caption * { color: #666; } .swiper-viewModal .swiper-caption .swiper-title { font-family: 'Josefin Sans', sans-serif; font-size: 45px; font-size: clamp(25.9px, calc(25.9px + ((1vw - 3.6px) * 2.98438)), 45px); min-height: 0vw; color: hsla(0, 0%, 44%, 1); /* Flex: order */ order: 1; } .swiper-viewModal .swiper-caption .swiper-title, .swiper-viewModal .swiper-caption .swiper-title a { font-weight: 600; border-bottom: none; color: var(--c-magique_p) !important; } .swiper-viewModal .swiper-caption .swiper-meta-fields { /* Flex: order */ order: 2; font-size: 13px; color: var(--c-magique_p); /* 区切りのカンマ・スラッシュを非表示にする */ font-size: 0px; color: transparent; } /* 日付 */ .swiper-viewModal .swiper-meta-fields .entry-date { display: inline-block; vertical-align: middle; line-height: 1; font-size: 13px; padding-bottom: 0.1em; margin-right: 4px; } /* タグ */ .swiper-viewModal .swiper-meta-fields .terms a[class^="swiper-tax-"] { display: inline-block; vertical-align: middle; padding: 0.2em 0.4em 0.3em; border-radius: 2px; line-height: 1; font-size: 11px; text-align: center; background: hsla(0, 0%, 96%, 1); pointer-events: none; margin-right: 4px; } /* カテゴリー(カテゴリー名を「cat-***」にしておく) */ .swiper-viewModal .swiper-meta-fields .terms a[class^="swiper-tax-cat-"] { position: absolute; top: 0px; left: 0px; margin: auto; display: inline-block; width: auto; height: auto; padding: 0.6em 1em 0.6em; line-height: 1; font-size: 13px; text-align: center; font-weight: 600; background: var(--bgc-magique_accent); color: #FFF; border-radius: 0px; /* カテゴリだけリンクにする */ pointer-events: auto; } .swiper-viewModal .swiper-caption .swiper-content { /* Flex: order */ order: 3; margin-top: 0.4em; width: 100%; font-size: var(--va-saraciere-fz_smaller); color: var(--c-magique_p); } .swiper-viewModal .swiper-content .btn { margin-top: 15px; display: block; width: 250px; max-width: 90%; margin-left: auto; margin-right: auto; padding: 6px 12px; font-size: 14px; line-height: 1.44; font-weight: 400; text-align: center; white-space: nowrap; border-radius: 4px; } .swiper-viewModal .swiper-readmore.btn { background: var(--bgc-magique_base); color: #428bca; border-radius: 0px; border: 1px solid var(--bgc-magique_accent); } .swiper-viewModal a:hover.swiper-readmore.btn { background: #428bca; color: #FFF; } /* navigation */ .swiper-viewModal .swiper-navigation { position: absolute; z-index: 10; top: 0; bottom: 0; left: 0px; right: 15px; margin: auto; width: 100%; max-width: 90vw; height: auto; } .swiper-viewModal .swiper-button-prev, .swiper-viewModal .swiper-button-next { display: inline-flex; justify-content: center; align-items: center; transform: translateZ(0); transition: all 0.1s ease-out; width: 74px; height: 74px; font-size: 74px; text-align: center; text-shadow: 0 1px 2px rgba(0, 0, 0, .6); border-radius: 999px; opacity: 1; background: transparent; color: hsla(0, 0%, 44%, 1); } .swiper-viewModal .swiper-button-prev { position: absolute; top: 0px; bottom: 0px; left: -12px; margin: auto; } .swiper-viewModal .swiper-button-next { position: absolute; top: 0px; bottom: 0px; right: -12px; margin: auto; } .swiper-viewModal .swiper-button-prev::before, .swiper-viewModal .swiper-button-next::before { font-family: 'Material Symbols Sharp'; font-variation-settings: 'FILL'0, 'wght'200; margin: auto; width: 1em; height: 1em; line-height: 1; font-size: 1em; text-align: center; color: inherit; } .swiper-viewModal .swiper-button-prev::before { content: "\e5cb"; } .swiper-viewModal .swiper-button-next::before { content: "\e5cc"; } @media (any-hover: hover) { .swiper-viewModal .swiper-button-prev:hover { opacity: 1; transform: translateX(-4px); } .swiper-viewModal .swiper-button-next:hover { opacity: 1; transform: translateX(4px); } } /* pagination */ .swiper-viewModal .swiper-pagination { position: static; margin-left: auto; margin-right: auto; /* navigation分の余白調整 */ padding: 15px 0px 15px 0px; } .swiper-viewModal .swiper-pagination-bullet { display: inline-block; vertical-align: middle; width: 10px; height: 10px; border-radius: 999px; background: #CECECE; border: 1px solid #CECECE; opacity: 1; } .swiper-viewModal .swiper-pagination-bullet-active { width: 12px; height: 12px; background: #428bca; opacity: 1; } </code></pre> <p> </p> <pre class="language-php"><code> <?php //(shortcode.php) /* ショートコード:コンポーネント -------------------------------------- */ //shortcode [masonry id= view=""]~[/masonry] function shortcode_masonry($atts) { extract(shortcode_atts(array( 'id' => '', 'view' => '', 'ids' => '' ), $atts)); $gallery_items = ''; $ids_array = explode(',', $ids); foreach ($ids_array as $id) { $gallery_item = wp_get_attachment_image($id, 'full'); $gallery_items .= '<li class="masonry-slide"><figure class="masonry-ifield"><a class="masonry-href-thumbnail ev-none">' . $gallery_item . '</a></figure></li>'; } $output = ''; //masonry-container $output .= '<div id="' . esc_attr($atts['id']) . '" class="masonry-container masonry-view' . esc_attr($atts['view']) . '">'; $output .= '<ul class="masonry-wrapper">'; $output .= $gallery_items; $output .= '</ul><!-- /.masonry-wrapper -->'; $output .= '</div><!-- /.masonry-container -->'; return $output; } add_shortcode('masonry', 'shortcode_masonry'); <?php //(shortcode.php) /* ショートコード:コンポーネント -------------------------------------- */ //shortcode [masonry id= view=""]~[/masonry] function shortcode_masonry($atts) { extract(shortcode_atts(array( 'id' => '', 'view' => '', 'ids' => '' ), $atts)); $gallery_items = ''; $ids_array = explode(',', $ids); foreach ($ids_array as $id) { $gallery_item = wp_get_attachment_image($id, 'full'); $gallery_items .= '<li class="masonry-slide"><figure class="masonry-ifield"><a class="masonry-href-thumbnail ev-none">' . $gallery_item . '</a></figure></li>'; } $output = ''; //masonry-container $output .= '<div id="' . esc_attr($atts['id']) . '" class="masonry-container masonry-view' . esc_attr($atts['view']) . '">'; $output .= '<ul class="masonry-wrapper">'; $output .= $gallery_items; $output .= '</ul><!-- /.masonry-wrapper -->'; $output .= '</div><!-- /.masonry-container -->'; return $output; } add_shortcode('masonry', 'shortcode_masonry'); //shortcode [swiper id="" view=""]~[/swiper] function shortcode_swiper($atts) { extract(shortcode_atts(array( 'id' => '', 'view' => '', 'ids' => '' ), $atts)); $gallery_items = ''; $ids_array = explode(',', $ids); foreach ($ids_array as $id) { $caption = $attachment->post_content; //キャプションを取得 $gallery_item = wp_get_attachment_image($id, 'full'); if ($caption) { $gallery_caption .= '<figcaption class="swiper-caption">' . $caption . '</figcaption>'; // キャプションがある場合はスライドに追加 } $gallery_items .= '<li class="swiper-slide"><figure class="swiper-ifield"><a class="swiper-href-thumbnail" href="' . wp_get_attachment_url($id) . '">' . $gallery_item . '</a>' . $gallery_caption . '</figure></li>'; } $output = ''; $output .= '<div class="swiper-modalOuter js-closeModal"></div>'; //swiper-container $output .= '<div id="' . esc_attr($atts['id']) . '" class="swiper-container swiper-view' . esc_attr($atts['view']) . '">'; $output .= '<a class="swiper-modalClose js-closeModal" aria-label="閉じる">×</a>'; $output .= '<div class="swiper-wrapper">'; $output .= $gallery_items; $output .= '</div><!-- /.swiper-wrapper -->'; //Swiper Pagination, Navigation $output .= '<div class="swiper-pagination"></div>'; $output .= '<div class="swiper-button-prev"></div><div class="swiper-button-next"></div>'; $output .= '</div><!-- /.swiper-container -->'; return $output; } add_shortcode('swiper', 'shortcode_swiper'); </code></pre> <p> </p> <p>この記事では、WordPressのショートコードを使ってMasonryとSwiperを実装する方法について詳しく解説しました。MasonryとSwiperを使うことで、簡単に美しいレイアウトやスライダーを作成することができます。ぜひ、実装してみてください。</p>
  • function shortcode_swiper($atts) { extract(shortcode_atts(array( 'id' => '', 'view' => '', 'ids' => '' ), $atts)); $gallery_items = ''; $ids_array = explode(',', $ids); foreach ($ids_array as $id) { $caption = $attachment->post_content; //キャプションを取得 $gallery_item = wp_get_attachment_image($id, 'full'); if ($caption) { $gallery_caption .= '
    ' . $caption . '
    '; // キャプションがある場合はスライドに追加 } $gallery_items .= '
  • ' . $gallery_item . '' . $gallery_caption . '
  • '; } $output = ''; $output .= '
    '; //swiper-container $output .= '
    '; $output .= '×'; $output .= '
    '; $output .= $gallery_items; $output .= '
    '; //Swiper Pagination, Navigation $output .= '
    '; $output .= '
    '; $output .= '
    '; return $output; } add_shortcode('swiper', 'shortcode_swiper');

     

    この記事では、WordPressのショートコードを使ってMasonryとSwiperを実装する方法について詳しく解説しました。MasonryとSwiperを使うことで、簡単に美しいレイアウトやスライダーを作成することができます。ぜひ、実装してみてください。