質問編集履歴

2

試したことを追記しました。

2018/07/12 11:33

投稿

mio_murata
mio_murata

スコア8

test CHANGED
File without changes
test CHANGED
@@ -276,6 +276,38 @@
276
276
 
277
277
 
278
278
 
279
+
280
+
281
+ 2018/7/12 17:55 更に追記です。
282
+
283
+ footer.phpのコードを一つずつ消してどこの部分が読み込まれていないか確認したところ、以下の部分を削除すると表示されず、他の部分を削除しても読み込まれました。
284
+
285
+ ```PHP
286
+
287
+ <script type="text/javascript">
288
+
289
+ (function($){
290
+
291
+ $('#toc').toc({
292
+
293
+ 'selectors': 'h3', //目次として表示する要素のCSSセレクターを指定
294
+
295
+ 'anchorName': function(i, heading, prefix) { //アンカーネームのカスタマイズ
296
+
297
+ return prefix+i;
298
+
299
+ },
300
+
301
+ });
302
+
303
+ })(jQuery);
304
+
305
+ </script>
306
+
307
+ ```
308
+
309
+
310
+
279
311
  ### 試したこと
280
312
 
281
313
 

1

新たに試したコードを追記しました。

2018/07/12 11:33

投稿

mio_murata
mio_murata

スコア8

test CHANGED
File without changes
test CHANGED
@@ -178,7 +178,7 @@
178
178
 
179
179
  ```
180
180
 
181
-
181
+ プラグインのフォルダの構成
182
182
 
183
183
  ```
184
184
 
@@ -192,7 +192,87 @@
192
192
 
193
193
  ```
194
194
 
195
-
195
+ 以下、追記です。
196
+
197
+ こちらはfooter.phpの</body>の直前に追加した内容です。これだと正しく目次が表示されます。
198
+
199
+ ```php
200
+
201
+ <div id="toc"></div>
202
+
203
+ <script src="https://example.com/wp-content/themes/Example/js/toc.min.js" type="text/javascript"></script>
204
+
205
+ <script type="text/javascript">
206
+
207
+ (function($){
208
+
209
+ $('#toc').toc({
210
+
211
+ 'selectors': 'h3', //目次として表示する要素のCSSセレクターを指定
212
+
213
+ 'anchorName': function(i, heading, prefix) { //アンカーネームのカスタマイズ
214
+
215
+ return prefix+i;
216
+
217
+ },
218
+
219
+ });
220
+
221
+ })(jQuery);
222
+
223
+ </script>
224
+
225
+ ```
226
+
227
+ その後、また新たにいくつかの方法を試しました。まず、Jqueryの読み込みの方法を以下のようにしました。
228
+
229
+ ```PHP
230
+
231
+ function load_scripts2(){
232
+
233
+ wp_enqueue_script(
234
+
235
+ 'toc2',
236
+
237
+ plugins_url('js/toc.js', __FILE__ ),
238
+
239
+ array( 'jquery' ),
240
+
241
+ filemtime( plugin_dir_path( __FILE__ ) . '/js/toc.js' ),
242
+
243
+ false
244
+
245
+ );
246
+
247
+ }
248
+
249
+ add_action( 'wp_enqueue_scripts', 'load_scripts2' );
250
+
251
+
252
+
253
+ function load_scripts(){
254
+
255
+ wp_enqueue_script(
256
+
257
+ 'toc',
258
+
259
+ plugins_url('js/toc.min.js', __FILE__ ),
260
+
261
+ array( 'jquery' ),
262
+
263
+ filemtime( plugin_dir_path( __FILE__ ) . '/js/toc.min.js' ),
264
+
265
+ false
266
+
267
+ );
268
+
269
+ }
270
+
271
+ add_action( 'wp_enqueue_scripts', 'load_scripts' );
272
+
273
+ ```
274
+
275
+ 先程同様、開発ツールでも読み込まれていて、見ることができました。ですが、目次は表示されていません。この、footer.phpに書き込むのとPHPに書き込むので、何か違う点はあるのでしょうか?
196
276
 
197
277
 
198
278