質問編集履歴

5

コードの修正

2024/03/06 10:57

投稿

bokupiroki
bokupiroki

スコア54

test CHANGED
File without changes
test CHANGED
@@ -38,9 +38,6 @@
38
38
 
39
39
  ### 試したこと
40
40
 
41
-
42
- ・●●●●
43
- ![●●●●](●●●●)
44
41
 
45
42
  ・エラー内容について
46
43
  該当ページに「Cannot redeclare current_crumb_tag()」というエラーがあることから、関数の重複を疑いました。

4

コードの修正

2024/03/06 10:56

投稿

bokupiroki
bokupiroki

スコア54

test CHANGED
File without changes
test CHANGED
@@ -1,317 +1,145 @@
1
1
  ### 前提・実現したいこと
2
-
3
2
  カスタム投稿タイプでページを作成したいです。
4
-
5
3
  しかし、アクセスするとエラーが出てページの内容を表示できません。
6
-
7
-
8
4
 
9
5
  この症状は「the thor」という特定のテーマだけで起こります。
10
6
 
11
-
12
-
13
7
  ### 発生している問題・エラーメッセージ
14
-
15
8
  作成したカスタム投稿のURLにアクセスするとエラーが出ます。
16
9
 
17
-
18
-
19
10
  ```
20
-
21
- Fatal error: Cannot redeclare current_crumb_tag() (previously declared in /home/●●●●/www/●●●●/wp-content/themes/the-thor/inc/front/breadcrumb.php:22) in /home/●●●●/www/●●●●/wp-content/themes/the-thor/inc/front/breadcrumb.php on line 22
22
-
11
+ Fatal error: Cannot redeclare current_crumb_tag() (previously declared in /home/www/fuga/wp-content/themes/the-thor/inc/front/breadcrumb.php:22) in /home/www/fuga/wp-content/themes/the-thor/inc/front/breadcrumb.php on line 22
23
12
  ```
24
13
 
25
14
 
26
-
27
- 実際のページはこれです。
28
-
29
- https://●●●●.●●●●.ne.jp/●●●●/example/aaa/
30
-
31
-
32
-
33
15
  ### 該当のソースコード
34
-
35
16
  子テーマのfunctions.phpに下記のコードを追加してカスタム投稿タイプの追加を行いました。
36
17
 
37
-
38
-
39
18
  ```php
40
-
41
19
  function create_post_type() {
42
-
43
20
  $exampleSupports = [ // supports のパラメータを設定する配列(初期値だと title と editor のみ投稿画面で使える)
44
-
45
21
  'title', // 記事タイトル
46
-
47
22
  'editor', // 記事本文
48
-
49
23
  'thumbnail', // アイキャッチ画像
50
-
51
24
  'revisions' // リビジョン
52
-
53
25
  ];
54
-
55
26
  register_post_type( 'example', // カスタム投稿名
56
-
57
27
  array(
58
-
59
28
  'label' => 'テストカスタム投稿', // 管理画面の左メニューに表示されるテキスト
60
-
61
29
  'public' => true, // 投稿タイプをパブリックにするか否か
62
-
63
30
  'has_archive' => true, // アーカイブを有効にするか否か
64
-
65
31
  'menu_position' => 5, // 管理画面上でどこに配置するか今回の場合は「投稿」の下に配置
66
-
67
32
  'supports' => $exampleSupports // 投稿画面でどのmoduleを使うか的な設定
68
-
69
33
  )
70
-
71
34
  );
72
-
73
35
  }
74
-
75
36
  add_action( 'init', 'create_post_type' ); // アクションに上記関数をフックします
76
-
77
37
  ```
78
-
79
-
80
38
 
81
39
  ### 試したこと
82
40
 
83
- ・カスタム投稿タイプのアーカイブページにはアクセスできました。
84
-
85
- https://●●●●.●●●●.ne.jp/●●●●/example/
86
-
87
-
88
41
 
89
42
  ・●●●●
90
-
91
43
  ![●●●●](●●●●)
92
44
 
93
-
94
-
95
45
  ・エラー内容について
96
-
97
46
  該当ページに「Cannot redeclare current_crumb_tag()」というエラーがあることから、関数の重複を疑いました。
98
-
99
47
  しかし、functions.phpに追記した関数名の「create_post_type」を別の文字に置き換えても結果は同じでした。
100
48
 
101
49
 
102
50
 
103
- そもそもエラーにある「current_crumb_tag()」という関を私は自分で定義していす。
51
+ breadcrumb.php全体のコードは文字オーバーにるの
52
+ ここには記載できませんでした。
104
53
 
105
-
54
+ ###追加で試したこと1
106
-
107
- 「in /home/●●●●/www/●●●●/wp-content/themes/the-thor/inc/front/breadcrumb.php on line 22」
55
+ hayato7様の回答をもとに、
108
-
109
- ともエラーにあることから、親テーマの該当ファイルの22行目を確認すると
56
+ 親テーマの該当関数が定義されているファイル
57
+ 「if ( function_exists( 'current_crumb_tag' )」
58
+ のようなif文を追記しました。
110
59
 
111
60
  ```php
112
-
113
- function current_crumb_tag( $current_permalink, $current_text = '', $args = array(), $current_class = 'breadcrumb__item breadcrumb__item-current' ) {
114
-
115
- ```
116
-
117
- と書かれていました。
118
-
119
- なぜここがエラーに出てくるのかは予想がつきません。
120
-
121
-
122
-
123
- breadcrumb.php全体のコードは文字数オーバーになるので
124
-
125
- ここには記載できませんでした。
126
-
127
-
128
-
129
- ###追加で試したこと1
130
-
131
- hayato7様の回答をもとに、
132
-
133
- 親テーマの該当関数が定義されているファイルに
134
-
135
- 「if ( function_exists( 'current_crumb_tag' )」
136
-
137
- のようなif文を追記しました。
138
-
139
-
140
-
141
- ```php
142
-
143
61
  //breadcrumb.php
144
-
145
-
146
62
 
147
63
  //中略
148
64
 
149
65
 
150
-
151
-
152
-
153
66
  if ( function_exists( 'current_crumb_tag' ) ) {
154
-
155
67
  function current_crumb_tag( $current_permalink, $current_text = '', $args = array(), $current_class = 'breadcrumb__item breadcrumb__item-current' ) {
156
-
157
68
  $args = wp_parse_args( $args );
158
-
159
69
  $args = ( object )$args;
160
-
161
70
  $current_class = $current_class ? ' class="' . esc_attr( $current_class ) . '"': '';
162
-
163
71
  $start_anchor_tag = $current_permalink ? '<a href="' . $current_permalink . '">': '';
164
-
165
72
  $end_anchor_tag = $current_permalink ? '</a>' : '';
166
-
167
73
  $current_before = '<li' . $current_class . '>' . $start_anchor_tag . '';
168
-
169
74
  $current_crumb_tag = $current_text;
170
-
171
75
  $current_after = '' . $end_anchor_tag . '</li>';
172
-
173
76
  if ( get_query_var( 'paged' ) ) {
174
-
175
77
  if ( is_paged() || is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) {
176
-
177
78
  $current_after = ' (ページ' . get_query_var( 'paged' ) . ')' . $current_after;
178
-
179
79
  }
180
80
 
181
-
182
-
183
81
  } elseif ( ( is_page() || is_single() ) && get_query_var( 'page' ) ) {
184
-
185
82
  $current_after = ' (ページ' . get_query_var( 'page' ) . ')' . $current_after;
186
-
187
83
  }
188
84
 
189
-
190
-
191
85
  return $current_before . $current_crumb_tag . $current_after;
192
-
193
86
  }
194
-
195
87
  }
196
-
197
-
198
88
 
199
89
  //中略
200
90
 
201
-
202
-
203
91
  //元から書いてあっためちゃくちゃ長いif文の途中
204
-
205
92
  } else {
206
-
207
93
  $post_type_object = get_post_type_object( get_post_type() );
208
-
209
94
  $breadcrumb_html .= '<li' . $li_class . '><a href="' . get_post_type_archive_link( get_post_type() ) . '">' . $post_type_object->label . '</a></li>';
210
-
211
95
  $taxonomies = get_object_taxonomies( get_post_type() );
212
-
213
96
  $category_term = '';
214
97
 
215
-
216
-
217
98
  foreach ( $taxonomies as $taxonomy ) {
218
-
219
99
  $taxonomy_obj = get_taxonomy( $taxonomy );
220
-
221
100
  if ( true == $taxonomy_obj->hierarchical ) {
222
-
223
101
  $category_term = $taxonomy_obj;
224
-
225
102
  break;
226
-
227
103
  }
228
-
229
104
  }
230
105
 
231
-
232
-
233
106
  if ( $category_term ) {
234
-
235
107
  $terms = get_the_terms( $post->ID, $category_term->name );
236
108
 
237
-
238
-
239
109
  if ( $terms ) {
240
-
241
110
  if ( !$terms || is_wp_error( $terms ) )
242
-
243
111
  $terms = array();
244
112
 
245
-
246
-
247
113
  $terms = array_values( $terms );
248
-
249
114
  $term = $terms[ 0 ];
250
115
 
251
-
252
-
253
116
  if ( $term->parent != 0 ) {
254
-
255
117
  $ancestors = array_reverse( get_ancestors( $term->term_id, $term->taxonomy ) );
256
-
257
118
  foreach ( $ancestors as $ancestor ) {
258
-
259
119
  $breadcrumb_html .= '<li' . $li_class . '><a href="' . get_term_link( $ancestor, $term->taxonomy ) . '">' . get_term( $ancestor, $term->taxonomy )->name . '</a></li>';
260
-
261
120
  }
262
-
263
121
  }
264
-
265
122
  //156行目↓↓
266
-
267
123
  $breadcrumb_html .= '<li' . $li_class . '><a href="' . get_term_link( $term, $term->taxonomy ) . '">' . $term->name . '</a></li>';
268
-
269
124
  }
270
-
271
125
  }
272
126
 
273
-
274
-
275
127
  $breadcrumb_html .= current_crumb_tag( get_the_permalink( $single->ID ), get_the_title( $single->ID ) );
276
-
277
128
  }
278
-
279
129
  //中略
280
-
281
- ```
282
-
283
- すると今度は次のようなエラーに変わりました。
284
-
285
- ```
286
-
287
- Fatal error: Uncaught Error: Call to undefined function current_crumb_tag() in /home/●●●●/www/●●●●/wp-content/themes/the-thor/inc/front/breadcrumb.php:156 Stack trace: #0 /home/●●●●/www/●●●●/wp-content/themes/the-thor/header.php(273): fit_breadcrumb() #1 /home/●●●●/www/●●●●/wp-includes/template.php(723): require_once('/home/smart-mob...') #2 /home/●●●●/www/●●●●/wp-includes/template.php(672): load_template('/home/smart-mob...', true) #3 /home/●●●●/www/●●●●/wp-includes/general-template.php(41): locate_template(Array, true) #4 /home/●●●●/www/●●●●/wp-content/themes/the-thor/single.php(1): get_header() #5 /home/●●●●/www/●●●●/wp-includes/template-loader.php(106): include('/home/smart-mob...') #6 /home/●●●●/www/●●●●/wp-blog-header.php(19): require_once('/home/smart-mob...') #7 /home/●●●●/www/●●●●/index.php(17): require('/home/smart-mob...') #8 {main} thrown in /home/●●●●/www/●●●●/wp-content/themes/the-thor/inc/front/breadcrumb.php on line 156
288
-
289
130
  ```
290
131
 
291
132
  「breadcrumb.php on line 156」と末尾にあることから今度は同じファイルの156行目付近に何かあるのか探してみましたが、よくわかりませんでした。(上記に周辺コードも載せています)
292
-
293
-
294
133
 
295
134
  関数名を定義する部分だけでなく、途中に登場する「$current_crumb_tag」にも何か処理を加えないといけないのでしょうか
296
135
 
297
136
 
298
137
 
299
-
300
-
301
-
302
-
303
138
  ### 補足情報(FW/ツールのバージョンなど)
304
-
305
139
  ・WordPress5.4.2
306
-
307
140
  ・PHP 7.3.18
308
-
309
141
  ・プラグインはすべて停止状態
310
-
311
-
312
142
 
313
143
  有料テーマなのでサポートにも問い合わせていますが、回答が得られていない状態です・・・
314
144
 
315
-
316
-
317
145
  どなたか知恵を貸していただけないでしょうか。

3

試したことをもう少しわかりやすく修正

2020/06/22 11:28

投稿

bokupiroki
bokupiroki

スコア54

test CHANGED
File without changes
test CHANGED
@@ -128,7 +128,15 @@
128
128
 
129
129
  ###追加で試したこと1
130
130
 
131
+ hayato7様の回答をもとに、
132
+
131
- 親テーマの該当関数が定義されているファイルにif文を追記しました。
133
+ 親テーマの該当関数が定義されているファイルに
134
+
135
+ 「if ( function_exists( 'current_crumb_tag' )」
136
+
137
+ のようなif文を追記しました。
138
+
139
+
132
140
 
133
141
  ```php
134
142
 
@@ -192,7 +200,7 @@
192
200
 
193
201
 
194
202
 
195
- //めちゃくちゃ長いif文の途中
203
+ //元から書いてあっためちゃくちゃ長いif文の途中
196
204
 
197
205
  } else {
198
206
 

2

試したことの追加

2020/06/22 11:28

投稿

bokupiroki
bokupiroki

スコア54

test CHANGED
File without changes
test CHANGED
@@ -126,6 +126,172 @@
126
126
 
127
127
 
128
128
 
129
+ ###追加で試したこと1
130
+
131
+ 親テーマの該当関数が定義されているファイルにif文を追記しました。
132
+
133
+ ```php
134
+
135
+ //breadcrumb.php
136
+
137
+
138
+
139
+ //中略
140
+
141
+
142
+
143
+
144
+
145
+ if ( function_exists( 'current_crumb_tag' ) ) {
146
+
147
+ function current_crumb_tag( $current_permalink, $current_text = '', $args = array(), $current_class = 'breadcrumb__item breadcrumb__item-current' ) {
148
+
149
+ $args = wp_parse_args( $args );
150
+
151
+ $args = ( object )$args;
152
+
153
+ $current_class = $current_class ? ' class="' . esc_attr( $current_class ) . '"': '';
154
+
155
+ $start_anchor_tag = $current_permalink ? '<a href="' . $current_permalink . '">': '';
156
+
157
+ $end_anchor_tag = $current_permalink ? '</a>' : '';
158
+
159
+ $current_before = '<li' . $current_class . '>' . $start_anchor_tag . '';
160
+
161
+ $current_crumb_tag = $current_text;
162
+
163
+ $current_after = '' . $end_anchor_tag . '</li>';
164
+
165
+ if ( get_query_var( 'paged' ) ) {
166
+
167
+ if ( is_paged() || is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) {
168
+
169
+ $current_after = ' (ページ' . get_query_var( 'paged' ) . ')' . $current_after;
170
+
171
+ }
172
+
173
+
174
+
175
+ } elseif ( ( is_page() || is_single() ) && get_query_var( 'page' ) ) {
176
+
177
+ $current_after = ' (ページ' . get_query_var( 'page' ) . ')' . $current_after;
178
+
179
+ }
180
+
181
+
182
+
183
+ return $current_before . $current_crumb_tag . $current_after;
184
+
185
+ }
186
+
187
+ }
188
+
189
+
190
+
191
+ //中略
192
+
193
+
194
+
195
+ //めちゃくちゃ長いif文の途中
196
+
197
+ } else {
198
+
199
+ $post_type_object = get_post_type_object( get_post_type() );
200
+
201
+ $breadcrumb_html .= '<li' . $li_class . '><a href="' . get_post_type_archive_link( get_post_type() ) . '">' . $post_type_object->label . '</a></li>';
202
+
203
+ $taxonomies = get_object_taxonomies( get_post_type() );
204
+
205
+ $category_term = '';
206
+
207
+
208
+
209
+ foreach ( $taxonomies as $taxonomy ) {
210
+
211
+ $taxonomy_obj = get_taxonomy( $taxonomy );
212
+
213
+ if ( true == $taxonomy_obj->hierarchical ) {
214
+
215
+ $category_term = $taxonomy_obj;
216
+
217
+ break;
218
+
219
+ }
220
+
221
+ }
222
+
223
+
224
+
225
+ if ( $category_term ) {
226
+
227
+ $terms = get_the_terms( $post->ID, $category_term->name );
228
+
229
+
230
+
231
+ if ( $terms ) {
232
+
233
+ if ( !$terms || is_wp_error( $terms ) )
234
+
235
+ $terms = array();
236
+
237
+
238
+
239
+ $terms = array_values( $terms );
240
+
241
+ $term = $terms[ 0 ];
242
+
243
+
244
+
245
+ if ( $term->parent != 0 ) {
246
+
247
+ $ancestors = array_reverse( get_ancestors( $term->term_id, $term->taxonomy ) );
248
+
249
+ foreach ( $ancestors as $ancestor ) {
250
+
251
+ $breadcrumb_html .= '<li' . $li_class . '><a href="' . get_term_link( $ancestor, $term->taxonomy ) . '">' . get_term( $ancestor, $term->taxonomy )->name . '</a></li>';
252
+
253
+ }
254
+
255
+ }
256
+
257
+ //156行目↓↓
258
+
259
+ $breadcrumb_html .= '<li' . $li_class . '><a href="' . get_term_link( $term, $term->taxonomy ) . '">' . $term->name . '</a></li>';
260
+
261
+ }
262
+
263
+ }
264
+
265
+
266
+
267
+ $breadcrumb_html .= current_crumb_tag( get_the_permalink( $single->ID ), get_the_title( $single->ID ) );
268
+
269
+ }
270
+
271
+ //中略
272
+
273
+ ```
274
+
275
+ すると今度は次のようなエラーに変わりました。
276
+
277
+ ```
278
+
279
+ Fatal error: Uncaught Error: Call to undefined function current_crumb_tag() in /home/●●●●/www/●●●●/wp-content/themes/the-thor/inc/front/breadcrumb.php:156 Stack trace: #0 /home/●●●●/www/●●●●/wp-content/themes/the-thor/header.php(273): fit_breadcrumb() #1 /home/●●●●/www/●●●●/wp-includes/template.php(723): require_once('/home/smart-mob...') #2 /home/●●●●/www/●●●●/wp-includes/template.php(672): load_template('/home/smart-mob...', true) #3 /home/●●●●/www/●●●●/wp-includes/general-template.php(41): locate_template(Array, true) #4 /home/●●●●/www/●●●●/wp-content/themes/the-thor/single.php(1): get_header() #5 /home/●●●●/www/●●●●/wp-includes/template-loader.php(106): include('/home/smart-mob...') #6 /home/●●●●/www/●●●●/wp-blog-header.php(19): require_once('/home/smart-mob...') #7 /home/●●●●/www/●●●●/index.php(17): require('/home/smart-mob...') #8 {main} thrown in /home/●●●●/www/●●●●/wp-content/themes/the-thor/inc/front/breadcrumb.php on line 156
280
+
281
+ ```
282
+
283
+ 「breadcrumb.php on line 156」と末尾にあることから今度は同じファイルの156行目付近に何かあるのか探してみましたが、よくわかりませんでした。(上記に周辺コードも載せています)
284
+
285
+
286
+
287
+ 関数名を定義する部分だけでなく、途中に登場する「$current_crumb_tag」にも何か処理を加えないといけないのでしょうか
288
+
289
+
290
+
291
+
292
+
293
+
294
+
129
295
  ### 補足情報(FW/ツールのバージョンなど)
130
296
 
131
297
  ・WordPress5.4.2

1

プラグインは全部停止している旨を追記

2020/06/22 11:14

投稿

bokupiroki
bokupiroki

スコア54

test CHANGED
File without changes
test CHANGED
@@ -132,6 +132,8 @@
132
132
 
133
133
  ・PHP 7.3.18
134
134
 
135
+ ・プラグインはすべて停止状態
136
+
135
137
 
136
138
 
137
139
  有料テーマなのでサポートにも問い合わせていますが、回答が得られていない状態です・・・