質問編集履歴

3

案件名を変更しました。

2019/09/28 23:27

投稿

Novz
Novz

スコア22

test CHANGED
@@ -1 +1 @@
1
- Wordpress functions.php で条件分岐す方法
1
+ Wordpress functions.php で Fatal error: Cannot redeclare がで
test CHANGED
File without changes

2

関数の二重定義エラーについて追記しました。

2019/09/28 23:27

投稿

Novz
Novz

スコア22

test CHANGED
File without changes
test CHANGED
@@ -142,7 +142,29 @@
142
142
 
143
143
 
144
144
 
145
+
146
+
147
+ 関数の二重定義エラーなので、function.phpの他の部分に
148
+
149
+
150
+
151
+ ```
152
+
153
+ function my_delete_plugin_files()
154
+
155
+ function my_remove_custom_background()
156
+
157
+ ```
158
+
159
+
160
+
161
+ がないか確認してみましたが見つかりませんでした。
162
+
163
+
164
+
145
- 条件分岐のコードを入れないと動作します。
165
+ 条件分岐のコードを入れないで上記を入れると動作します。
166
+
167
+
146
168
 
147
169
 
148
170
 
@@ -150,6 +172,8 @@
150
172
 
151
173
 
152
174
 
175
+
176
+
153
177
  どうかお力をおかしください。
154
178
 
155
179
  よろしくおねがいします。

1

functions.phpで条件分岐するコードは動作するようになりましたが中の関数が動作しません。ここまで書いたコードを記載しました。どうぞよろしくおねがいします。

2019/09/28 22:43

投稿

Novz
Novz

スコア22

test CHANGED
File without changes
test CHANGED
@@ -2,13 +2,19 @@
2
2
 
3
3
 
4
4
 
5
+ テーマ「twentyeleven」にて
6
+
5
- 固定ページのページテンプレート page-template.phpのみに、下記の関数を適用させたいと思っいます。
7
+ 固定ページのページテンプレート page-template.phpのみに、下記の関数を適用させて
8
+
9
+ twentyelevenのスタイルやカスタマイザーを適用しないようにしたいと思っています。
6
10
 
7
11
 
8
12
 
9
13
 
10
14
 
11
15
  ``` function.php
16
+
17
+ // wp_headの不要なコードを削除する
12
18
 
13
19
  function my_delete_plugin_files() {wp_dequeue_style('twentyeleven-block-style');}
14
20
 
@@ -17,6 +23,8 @@
17
23
 
18
24
 
19
25
 
26
+
27
+ // カスタマイザーが生成するcssを無効にする
20
28
 
21
29
  add_action( 'after_setup_theme', 'my_remove_custom_background', 11);
22
30
 
@@ -38,24 +46,110 @@
38
46
 
39
47
 
40
48
 
41
- ```
49
+ ``` function.php
42
-
43
- <? php if( is_page_template(‘/page-template.php')) ; ?>
44
50
 
45
51
 
46
52
 
53
+ function switch_twentyeleven_style() {
54
+
55
+ if( !is_page_template('page-template.php')){
56
+
57
+
58
+
59
+ // page-2019でない場合はデフォルトテンプレートを読み込む
60
+
61
+ wp_enqueue_style( 'twentyeleven_style', get_stylesheet_directory_uri() . '/style.css' );
62
+
63
+
64
+
65
+ } else {
66
+
47
- <?php else; ?>
67
+ // page-2019の場合
68
+
69
+
70
+
71
+ function my_delete_plugin_files() {wp_dequeue_style('twentyeleven-block-style');}
72
+
73
+ add_action( 'wp_enqueue_scripts', 'my_delete_plugin_files' );
48
74
 
49
75
 
50
76
 
77
+
78
+
79
+ add_action( 'after_setup_theme', 'my_remove_custom_background', 11);
80
+
51
- <?php endif; ?>
81
+ function my_remove_custom_background() {
82
+
83
+ remove_theme_support( 'custom-background');
84
+
85
+ }
86
+
87
+
88
+
89
+ }
90
+
91
+ }
92
+
93
+ add_action('wp_enqueue_scripts', 'switch_twentyeleven_style');
94
+
95
+
52
96
 
53
97
  ```
54
98
 
55
99
 
56
100
 
57
- 書いらエラー出てしまいました。
101
+ これでテンプレート条件分岐はできるようなっのです
58
102
 
59
103
 
60
104
 
105
+ ```
106
+
107
+ function my_delete_plugin_files() {wp_dequeue_style('twentyeleven-block-style');}
108
+
109
+ add_action( 'wp_enqueue_scripts', 'my_delete_plugin_files' );
110
+
111
+
112
+
113
+
114
+
115
+ add_action( 'after_setup_theme', 'my_remove_custom_background', 11);
116
+
61
- お手数をおかけしますがご教授いただけないでしょうか。どうぞよろしくおねがいします。
117
+ function my_remove_custom_background() {
118
+
119
+ remove_theme_support( 'custom-background');
120
+
121
+ }
122
+
123
+
124
+
125
+ ```
126
+
127
+
128
+
129
+ の部分で
130
+
131
+
132
+
133
+ ```
134
+
135
+ Fatal error: Cannot redeclare
136
+
137
+ ```
138
+
139
+
140
+
141
+ というエラーでてうまく動作しません。
142
+
143
+
144
+
145
+ 条件分岐のコードを入れないと動作します。
146
+
147
+
148
+
149
+ どのように修正すると動作するでしょうか。
150
+
151
+
152
+
153
+ どうかお力をおかしください。
154
+
155
+ よろしくおねがいします。