固定ページAと、Aの子ページaに 通常のページとは違うコンテンツを挿入したいと思っています。
A以外の固定ページは親子関係がありません。
参考として
https://www.genius-web.co.jp/blog/cat-101/page-is-ancestor.htmlと、
https://showjinx.hatenablog.com/entry/parent-slug-wordpress
内のコードをfunctions.phpにコピペして適宜スラッグや関数名だけ合わせたのですが、
参考URL1つ目の
PHP
1function page_is_ancestor_of($slug){ 2 if ( is_page() && $post->post_parent > 0 ) { 3 4 global $post; 5 6 $page = get_page_by_path($slug); 7 $result = false; 8 if(isset($page)){ 9 foreach ($post->ancestors as $ancestor) { 10 if($ancestor == $page->ID){ $result = true; } 11 } 12 } 13 return $result; 14 } 15 16}
だとページaの分岐部分に
Notice: Undefined variable: post in /*******/wp-content/themes/twentyseventeen/functions.php on line 上記コード開始行
Notice: Trying to get property 'post_parent' of non-object in /*******/wp-content/themes/twentyseventeen/functions.php on line 上記コード開始行
Notice: Undefined variable: post in /*******/wp-content/themes/twentyseventeen/functions.php on line 597
Notice: Trying to get property 'post_parent' of non-object in *******/wp-content/themes/twentyseventeen/functions.php on line 上記コード開始行
のエラーが表示され、ページAや他固定ページにはエラーは出ません。
参考URL2つ目のの
PHP
1 2/** 固定ページの親ページを判別して条件分岐 ex.Aページを親に持つ子ページすべてに同じものを表示させたい **/ 3function is_parent_slug() { 4 global $post; 5 if ($post->post_parent) { 6 $post_data = get_post($post->post_parent); 7 return $post_data->post_name; 8 } 9}
だと、Aとa以外の固定ページの分岐部分に
Notice: Trying to get property 'post_parent' of non-object in /*******/wp-content/themes/twentyseventeen/functions.php on line 上記コード開始行
Notice: Trying to get property 'post_parent' of non-object in /*******/wp-content/themes/twentyseventeen/functions.php on line 上記コード開始行
Notice: Trying to get property 'post_parent' of non-object in /*******/wp-content/themes/twentyseventeen/functions.php on line 上記コード開始行
のエラーが表示され、A・aでは出ません。
Wordpressのバージョンは4.9.8
PHPは7.2.8です。
このエラーを解決する方法でも、他の手段でも、
このような表示の分け方を実現できる方法があれば
ご教示いただけると幸いです。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。