###目的
タグをフロントエンドで選択保存すべく、後述のようにwp_set_object_terms
を使っています。
機能の実装はできたのですが、タグ数が多いので保存の関数を一括させたいと考えています。
###現状のコード
現状は1つず保存しています。
php
1// 選択 2<input type="checkbox" name="one" value="one">one 3<input type="checkbox" name="two" value="two">two 4<input type="checkbox" name="three" value="three">three 5 6// 保存(1つずつ) 7if (isset($_POST) && $_POST['one']!='') { 8 wp_set_object_terms( $topic_id, 'one', 'custom-tag', true ); 9} 10if (isset($_POST) && $_POST['two']!='') { 11 wp_set_object_terms( $topic_id, 'two', 'custom-tag', true ); 12} 13if (isset($_POST) && $_POST['three']!='') { 14 wp_set_object_terms( $topic_id, 'three', 'custom-tag', true ); 15}
しかしタグはone
とtwo
とthree
だけでなくたくさんあるので、上の保存を一括させたいです。
###目的のためのコード
そこで保存の部分を配列で一括させるべく、次のようなコードに挑戦してみました。
php
1// 選択 2<input type="checkbox" name="custom-tag[]" value="one">one 3<input type="checkbox" name="custom-tag[]" value="two">two 4<input type="checkbox" name="custom-tag[]" value="three">three 5 6// 保存(一括) 7if (isset($_POST) && $_POST['custom-tag']!='') { 8 wp_set_object_terms( $post_id, 'custom-tag[]', 'custom-tag', true ); 9}
しかしこれではうまくいきませんでした。
たとえばone
とtwo
にチェックして保存しても、custom-tag[]
という文字がそのまま、custom-tag
のタグとして保存されてしまいます。
言うまでもなく保存したいのはone
とtwo
で、custom-tag[]
という文字列ではありません…。
そのためにどこを書き直せばいいか、もしお分かりになる方がいらっしゃいましたらご教示頂けましたら幸いです。
宜しくお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/15 03:34
2018/10/15 03:57