WP 特定カテゴリで使われているタグ一覧

タグ一覧を記述したいところに下記を記述

<?php
// 現在のカテゴリーのIDを取得
$cat_id = get_query_var('cat');
// 現在のカテゴリーに属する投稿のIDを配列で取得
$post_ids = get_objects_in_term($cat_id, 'category');
// 現在のカテゴリーに属する投稿で利用しているタグのオブジェクトを取得
$tags_object = wp_get_object_terms($post_ids, 'post_tag');
// タグのオブジェクトを利用しタグ一覧を作成
if ($tags_object) {
    echo '<ul class="tag-list">';
    foreach ($tags_object as $tag) {
        echo '<li><a href="' . get_tag_link($tag->term_id) . '">' . $tag->name . '</a></li>';
    }
    echo '</ul>';
}
?>

下記を参考にさせて頂きました。 https://blog-and-destroy.com/18588

gulpをプロジェクトで使う方法

LocalでWP themeで使う場合

1. testテーマ作成

2.gulpfile.js, package.json, package-lock.jsonをコピー

3.各ファイル変更

gulpfile.js(browserSyncOption の proxyを変更)

const browserSyncOption = {
  proxy: "test.local", //環境によって変更する
  open: true,
  reloadOnRestart: true,
  notify: false,
};

package.json, package-lock.json("name"を全て変更する3ヶ所)

"name": "test",

package.json のbrowserslistからie = 11`を削除する

4.gulpとパッケージをinstall

npm install

5. 必要なファイル作成

6. gulp実行

npx gulp

gulp導入

ディレクトリ作成

mkdir gulp

作成したディレクトリに移動

cd gulp

package.json作成

pakage.jsonとはプロジェクト情報を記載するファイル。package.jsonを作成するコマンドはnpm initです。作成の際に質問されるので全てイエスで回答する場合は-yをつけます。

npm init -y

vs codeでpackage.jsonを開く

code .

gulpインストール

インストール時にdependencies(サイト動作に関わるツール)とdevDependencies(開発時に必要になるツール)を選択できる。 -DをつけてdevDependenciesとしてインストール。

npm install gulp -D

エラーが出ますが、gulpのバージョンを確認します。

npx gulp -v

CLI version: 2.3.0
Local version: 4.0.2

バージョンが出るのでgulpのインストールはできている。CLIとLocalが一致していませんが、不具合ではない様です。

gulpの起動

gulp本体のファイルはプロジェクトフォルダ内の./node_modules/gulp/bin/gulp.jsです。これをコマンドで打てば実行できますが、非効率なので、パス指定を省くコマンドnpxを使います。node_modules内にあるプログラムを実行できます。

gulp実行・起動

npx gulp

[15:58:30] No gulpfile found

gulpファイルがないと警告

gulpfile.js作成

プロジェクトディレクトリ直下にgulpfile.jsを作成

touch gulpfile.js //ファイル作成
code gulpfile.js //vs codeで開く

再度npx gulpで起動

npx gulp     
    
[16:03:25] Using gulpfile ~/gulp/gulpfile.js
[16:03:25] Task never defined: default
[16:03:25] To list available tasks, try running: gulp --tasks

defaultのタスクが定義されていないと警告

タスクの定義

オブジェクトの読み込み

gulpで使う関数はオブジェクトにまとめられていて、glupモジュールに記載されている。 オブジェクトを使用するにはgulpモジュールを読み込む。

gulpfile.js

const gulp = require('gulp')

タスクの登録

gulp.task('タスク名',関数)で登録していきます。

gulpfile.js

gulp.task('default',()=>{})

これで実行すると

npx gulp

[16:15:05] Using gulpfile ~/gulp/gulpfile.js
[16:15:05] Starting 'default'...
[16:15:05] The following tasks did not complete: default
[16:15:05] Did you forget to signal async completion?

defaultタスクが完了していないと警告

gulpは非同期処理

非同期処理とはサーバがリクエストを処理して返してくれるまでの間に他の処理を進めること。非同期処理が完了した際に実行する関数をコールバック関数という。

gulpは非同期処理で動いています。

タスク完了

gulpfile.js

const gulp = require('gulp')

gulp.task('default',(done)=>{ done() })

gulp実行

npx gulp

[16:26:22] Using gulpfile ~/gulp/gulpfile.js
[16:26:22] Starting 'default'...
[16:26:22] Finished 'default' after 2.31 ms

処理の完了ができました!

Sassコンパイル

ファイル準備・作成

|_src
     |__sass
             |__common.scss

gulpfile.js

const gulp = require('gulp')

gulp.task('sass',()=>{ 
    gulp.src('./src/sass/common.scss')
})

gulp実行

npx gulp sass  
           
[16:42:28] Using gulpfile ~/gulp/gulpfile.js
[16:42:28] Starting 'sass'...
[16:42:28] The following tasks did not complete: sass
[16:42:28] Did you forget to signal async completion?

ここまでで、読み込んだscssファイルをストリームに変換する準備が整いました。

コンパイル

下記からsassとgulp-sassプラグインの両方をインストールします。

gulp-sass - npm

npm i gulp-sass -D

またエラーが出ていますがpackage.jsonに記載されているのでインストールされている。

  "devDependencies": {
    "gulp": "^4.0.2",
    "gulp-sass": "^5.1.0",
    "sass": "^1.56.1"
  }

sassとgulp-sass読み込み、pipe関数でsass()関数に渡されてコンパイル。 gulp.destメソッドで書き出しを指定

gulpfile.js

const gulp = require('gulp')
const sass = require('gulp-sass')(require('sass'))

gulp.task('sass',()=>{ 
    return gulp.src('./src/sass/common.scss')
    .pipe(sass())
    .pipe(gulp.dest('./dist'))
})

npx gulp sassでgulp実行

npx gulp sass

[17:00:30] Using gulpfile ~/gulp/gulpfile.js
[17:00:30] Starting 'sass'...
[17:00:30] Finished 'sass' after 22 ms

Finishedと出ているので、書き出しされているか確認するとdistフォルダ内にcommon.cssが生成

PostCSSを利用

PostCSSとPostCSSを使うためのgulp-postcssモジュール、両方インストール。

postcss - npm

gulp-postcss - npm

自動でベンダープレフィックスをつけるPostCSSプラグインAutoprefixerもインストール

autoprefixer - npm

browserlistを指定(package.json)

browserlistで対応するブラウザを指定

{
  "description": "初めてのgulp",
  "repository": "git@github.com:seiko-takamori/gulp_test.git",
  "license": "ISC",
  "devDependencies": {
    "autoprefixer": "^10.4.13",
    "gulp": "^4.0.2",
    "gulp-postcss": "^9.0.1",
    "gulp-sass": "^5.1.0",
    "postcss": "^8.4.19",
    "sass": "^1.56.1"
  },
  "browserslist": [
    "last 2 versions",
    "> 5%",
    "not ie <= 10",
    "ios >= 8",
    "and_chr >= 5",
    "Android >= 5"
  ]
}

タスク組み込み(gulpfile.js)

const gulp = require('gulp')
const sass = require('gulp-sass')(require('sass'))
const postcss = require('gulp-postcss') // postcss読み込み
const autoprefixer = require('autoprefixer') // autoprefixer読み込み

// autoprefixerのoptionを指定
const autoprefixerOption = {
    grid:true
}
const postcssOption = [autoprefixer(autoprefixerOption)]// gulp-postcssにプラグインを渡す配列を宣言(option指定あり)

gulp.task('sass',()=>{ 
    return gulp.src('./src/sass/common.scss')
    .pipe(sass())
    .pipe(postcss(postcssOption)) //postcssOptionをpostcss関数に渡す
    .pipe(gulp.dest('./dist'))
})

PostCSS Flexbugs Fixesでブラウザ差異を回避(gulpfile.js)

PostCSS Flexbugs Fixesをインストール

postcss-flexbugs-fixes - npm

const gulp = require('gulp')
const sass = require('gulp-sass')(require('sass'))
const postcss = require('gulp-postcss')
const autoprefixer = require('autoprefixer')
const flexBugsFixes = require('postcss-flexbugs-fixes')

const autoprefixerOption = {
    grid:true
}

const postcssOption = [autoprefixer(autoprefixerOption)]

gulp.task('sass',()=>{ 
    return gulp.src('./src/sass/common.scss')
    .pipe(sass())
    .pipe(postcss(postcssOption))
    .pipe(gulp.dest('./dist'))
})

自動でタスクを実行

・gulp.watchメソッドで監視 ・gulp.seriesメソッドでタスクをコマンドラインからではなく、関数から実行

const gulp = require('gulp')
const sass = require('gulp-sass')(require('sass'))
const postcss = require('gulp-postcss')
const autoprefixer = require('autoprefixer')
const flexBugsFixes = require('postcss-flexbugs-fixes')

const autoprefixerOption = {
    grid:true
}

const postcssOption = [autoprefixer(autoprefixerOption)]

gulp.task('sass',()=>{ 
    return gulp.src('./src/sass/common.scss')
    .pipe(sass())
    .pipe(postcss(postcssOption))
    .pipe(gulp.dest('./dist'))
})

gulp.task('watch',()=>{
    return gulp.watch('./src/sass/**/*.scss',gulp.series('sass'))
})

watchを実行 (終わる時はCtrl + C)

npx gulp watch

[18:32:26] Using gulpfile ~/gulp/gulpfile.js
[18:32:26] Starting 'watch'...
[18:32:30] Starting 'sass'...
[18:32:31] Finished 'sass' after 112 ms

WP JSファイル内でget_template_directory_uri();を使う

例えば、modal.jsというjsファイル内で画像のパスを書きたいとします。

1. <?= get_template_directory_uri(); ?>を変数に代入

jsコードの中で<?= get_template_directory_uri(); ?>が使えないので、jsファイルの前で<?= get_template_directory_uri(); ?>を変数に代入します。

<script>
   let path = '<?= get_template_directory_uri(); ?>';
</script>

<script src="<?= get_template_directory_uri(); ?>/js/modal.js"></script>

2. 変数に置き換え

$('li[data-tab="tab1"]').on('click', function() {
    $('.modal_over_img').attr('src', path + '/img/studio-list/map_visual.jpg');
});
    

これで無事にURL書き出しできました。

下記を参考にさせていただきました!感謝!!

【WordPress】JSファイル内でテーマフォルダまでのURLを使う方法 | ゆうやの雑記ブログ

sass baseサンプル fontサンプル

_base.scss

@use "color" as c;
@use "font" as t; //mean type

// base layout variables
$width_pc: 1536px;
$sideMargin_pc: 80px;
$width_sp: 768px;
$sideMargin_sp: 20px;

// tablet width
$width_tab: 900;

// margin size
$margin_s: 40;
$margin_m-: 60;
$margin_m: 100;
$margin_l: 180;

// default hover style
a:hover {
  opacity: 0.5;
}

// br
@media screen and (max-width: 768px) {
  .br_onPC {
    display: none;
  }
}
@media screen and (min-width: 769px) {
  .br_onSP {
    display: none;
  }
}

@function get_vw($size, $viewport: 375) {
  $rate: calc(100 / $viewport);
  @return $rate * $size * 1vw;
}

@function calc_fz($size, $viewport: 375) {
  $rate: calc(100 / $viewport);
  @return clamp(14px, #{$rate * $size}vw, #{$size}px);
}

@mixin font-rem($size, $base: 16) {
  font-size: $size + px;
  font-size: calc($size / $base) + rem;
}

// mixins
@mixin media-pc {
  @media screen and (min-width: calc($width_sp + 1px)) {
    @content;
  }
}

@mixin media-sp {
  @media screen and (max-width: $width_sp) {
    @content;
  }
}

@mixin media-tab {
  @media screen and (min-width: calc($width_sp + 1px)) and (max-width: calc($width_tab * 1px)) {
    @content;
  }
}

@mixin textLink_arrow {
  position: relative;
  &::after {
    content: "";
    position: absolute;
    width: calc(100% - 100px);
    height: 10px;
    background-image: url(../img/common/textLink_arrow.svg);
    background-size: contain;
    background-repeat: no-repeat;
    left: get_vw(-5, 1536);
    bottom: 5px;
    @include media-sp {
      width: 100%;
      left: 0;
      bottom: -2px;
    }
  }
}

//fade in up
.fade {
  opacity: 0;
  visibility: hidden;
  transform: translate(0, 20px);
  transition: 0.5s all ease-in;
  &.active {
    opacity: 1;
    visibility: visible;
    transform: translate(0, 0);
  }
}

_font_scss

// Serif jp (Noto Serif Base)
$SerifJP: "Noto Serif JP", serif;

// SanSerif jp (Noto Sans Base)
$SanSerifJP: "Noto Sans JP", sans-serif;

// Serif en (Cinzel Base)
$SerifEN: "Cinzel", serif;

$SanSerifEN: "Raleway", sans-serif;

// font mixins
@mixin cinzel {
  font-family: $SerifEN;
  font-weight: 400;
}

@mixin raleway_regular {
  font-family: $SanSerifEN;
  font-weight: 400;
}

@mixin raleway_semibold {
  font-family: $SanSerifEN;
  font-weight: 600;
}

@mixin SerifJP_light {
  font-family: $SerifJP;
  font-weight: 300;
}

@mixin SerifJP_regular {
  font-family: $SerifJP;
  font-weight: 400;
}

@mixin SerifJP_medium {
  font-family: $SerifJP;
  font-weight: 500;
}

@mixin SerifJP_bold {
  font-family: $SerifJP;
  font-weight: 700;
}

@mixin SanSerifJP_light {
  font-family: $SanSerifJP;
  font-weight: 300;
}

@mixin SanSerifJP_regular {
  font-family: $SanSerifJP;
  font-weight: 400;
}

@mixin SanSerifJP_medium {
  font-family: $SanSerifJP;
  font-weight: 500;
}

WP キャッシュしたくないページのheadに記述する

WPでソートをランドにしたら、ブラウザによってキャッシュが強く残り、固定表示の様になってしまった時に下記をhead内に記述することで、対応できたことがあります。

head内

    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Cache-Control" content="no-cache">

下記のサイトを参考にさせていただきました!感謝!!

www.tagindex.com

上記でもキャッシュが残ってしまう際にはさらに、下記を追加します。

<meta http-equiv="Expires" content=0>

こちらは下記を参考にさせていただきました。

mateタグ