WordPressの有料テーマ「SWELL」を使用している際、記事のカテゴリー名表示箇所に以下のエラーが表示される場合があります。
Warning:Undefined array key 0 in /***/themes/swell/classes/Utility/Get.php on line 805
目次
エラーの原因
このエラーが出る原因は以下の場合です。
- カスタムタクソノミーを設定している
- カスタムタクソノミーに階層を持たせている
エラーが出ているファイルは、SWELLの親テーマの中swell/classes/Utility/Get.php
805行目を見てみると・・・
// 階層を保つ場合は親から順に並べる
if ( is_taxonomy_hierarchical( $tax ) ) {
$term_tree = [];
foreach ( $terms as $term ) {
$self_id = $term->term_id;
$parent_id = $term->parent; $term_data = [
'id' => $term->term_id,
'slug' => $term->slug,
'name' => $term->name,
'url' => get_term_link( $term ),
];
$acts_ct = 0;
$top_act_id = $self_id;
if ( $parent_id ) {
// 先祖リストを取得
$ancestors = array_reverse( get_ancestors( $term->term_id, 'category' ) );
$acts_ct = count( $ancestors );
$top_act_id = $ancestors[0];
}$top_act_id = $ancestors[0];が取得できない状態です。
$ancestors は array_reverse( get_ancestors( $term->term_id, 'category' ) ); と指定されていますが、ここでcategoryのみが指定されているため、カスタムタクソノミーを設定している記事でエラーにるというわけです。
対処法
swell/classes/Utility/Get.phpのコードを修正する
805行目を書き換える対処法です。
SWELLの親テーマを変更することになるため、バージョンアップ時は上書きされて元に戻ることがある為、都度の書き換えが必要になります。
$ancestors = array_reverse( get_ancestors( $term->term_id, $term->taxonomy ) );Warning表示をさせないようにする
エラー表示(Warning:)を出力させないようにする設定があります。
エラーの根本的な解決ではありません。
その設定をすることにより、Warningの表示は消えますが、その他にエラーがある場合でも表示されなくなってしまいます。

