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?

WordPressにカスタムのjQueryのスクリプトを入れて動かす

Last updated at Posted at 2025-10-24

概要

「ぬる〜っとスクロールするアレにして!」と言う依頼
利用テーマはCocoon

wordpressのデフォルトではアンカージャンプのスクロールになっておらず
アンカージャンプを押下した際の挙動はブラウザのデフォルトのままとなっている。

手順

管理画面 > 外観 > テーマファイルエディタ > ▼tmp-user > footer-insert.php

footer-insert.php
###### 追加情報
<script>
	$(function() {
		$('a[href^="#"]').click(function () {
		  const speed = 800;
		  let href = $(this).attr("href");
		  let target = $(href == "#" || href == "" ? "html" : href);
		  let position = target.offset().top;
		  $("body,html").animate({ scrollTop: position }, speed, "swing");
		  return false;
		});
	});
</script>
######これがデフォルト部分
<?php
//フッター部分に解析タグを挿入したいときはこのテンプレートに挿入
//子テーマのカスタマイズ部分を最小限に抑えたい場合に有効なテンプレートとなります。
?>
<?php if (!is_user_administrator()) :
//管理者を除外してカウントする場合は以下に挿入 ?>

<?php endif; ?>
<?php //全ての訪問者をカウントする場合は以下に挿入 ?>

jsを実行するためにブロックにjsを埋め込む形で実現可能だった。
footer-insert.php自体は

/wp-content/themes/cocoon-child-master/tmp-user/footer-insert.php

に存在しておりファイルを編集しても良し。

と言う訳でぬる〜っとスクロールするアレの実装完了。

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?