WordPressテーマSANGOの質問

カスタム投稿タイプでパンくずの表示が「現在のページ名+サイト名」になってしまう

アバター
headcocoro

パンくずについての質問です。

カスタム投稿タイプ(例:event)を設定したのちの
===
問題:カスタム投稿アーカイブページ(例:event)
===
ではパンくずの表示が「現在のページ名+サイト名」になってしまいます。

また、
===
問題:そのカスタム投稿記事においては、
===
パンくずリスト自体が表示されません。

そして、
===
問題:カスタム投稿タイプのカテゴリーページにおいては「カテゴリーの編集」ページに遷移しても「ページタイトル」「メタデスクリプション」の入力フィールドを見つけることができない。
===

以上、カスタム投稿タイプ利用時のパンくず表示とカスタム投稿でどこまでSangoの基本設定が引き継がれるのか・引き継がれないのかについて教えて下さい。

SEOを考えたカテゴリー設定
SEOを考えたカテゴリー設定をしよう
コメントへの回答
サルワカくん
サルワカくん
2019/07/29

WordPressの仕組み的にカスタム投稿タイプが作られたときに、それをパンくずリストに自動的に組み込むことは難しいです。

SANGOでは、breadcrumb()という関数によりパンくずリストを出力しています(この関数はlibrary/functions/sng-functions.phpにかかれています)

カスタム投稿タイプでパンくずリストを出力するためには、こちらを子テーマのfunctions.phpでこちらを上書きする必要があります。

function breadcrumb() { global $post; $str = ''; if (!is_home() && !is_admin()) { // トップページ、管理画面では表示しない $str .= '<nav id="breadcrumb" class="breadcrumb"><ul itemscope itemtype="http://schema.org/BreadcrumbList">'; // ホームのぱんくず $str .= '<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a href="' . home_url() . '" itemprop="item"><span itemprop="name">ホーム</span></a><meta itemprop="position" content="1" /></li>'; if (is_category()) { // カテゴリーページ $cat = get_queried_object(); if ($cat->parent != 0) { $ancestors = array_reverse(get_ancestors($cat->cat_ID, 'category')); $i = 2; foreach ($ancestors as $ancestor) { $str .= '<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a href="' . esc_url(get_category_link($ancestor)) . '" itemprop="item"><span itemprop="name">' . esc_attr(get_cat_name($ancestor)) . '</span></a><meta itemprop="position" content="' . $i . '" /></li>'; $i++; } // end foreach } //END カテゴリー } elseif (is_tag()) { // タグページ $str .= '<li><i class="fa fa-tag"></i> タグ</li>'; } elseif (is_date()) { // 日付ページ if (is_day()) { $str .= '<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a href="' . get_year_link(get_query_var('year')) . '" itemprop="item"><span itemprop="name">' . get_query_var('year') . '年</span></a><meta itemprop="position" content="2" /></li>'; $str .= '<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a href="' . get_month_link(get_query_var('year'), get_query_var('monthnum')) . '" itemprop="item"><span itemprop="name">' . get_query_var('monthnum') . '月</span></a><meta itemprop="position" content="3" /></li>'; } elseif (is_month()) { $str .= '<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a href="' . get_year_link(get_query_var('year')) . '" itemprop="item"><span itemprop="name">' . get_query_var('year') . '年</span></a><meta itemprop="position" content="2" /></li>'; } } elseif (is_author()) { // 著者ページ $str .= '<li>著者</li>'; } elseif (is_page()) { if ($post->post_parent != 0) { $ancestors = array_reverse(get_post_ancestors($post->ID)); $i = 2; foreach ($ancestors as $ancestor) { $str .= '<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a href="' . esc_url(get_permalink($ancestor)) . '" itemprop="item"><span itemprop="name">' . esc_attr(get_the_title($ancestor)) . '</span></a><meta itemprop="position" content="' . $i . '" /></li>'; $i++; } } } elseif (is_single()) { // 投稿ページ $categories = get_the_category($post->ID); if ($categories) { $cat = $categories[0]; $i = 2; if ($cat->parent != 0) { $ancestors = array_reverse(get_ancestors($cat->cat_ID, 'category')); foreach ($ancestors as $ancestor) { $str .= '<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a href="' . esc_url(get_category_link($ancestor)) . '" itemprop="item"><span itemprop="name">' . esc_attr(get_cat_name($ancestor)) . '</span></a><meta itemprop="position" content="' . $i . '" /></li>'; $i++; } } $str .= '<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a href="' . esc_url(get_category_link($cat->term_id)) . '" itemprop="item"><span itemprop="name">' . esc_attr($cat->cat_name) . '</span></a><meta itemprop="position" content="' . $i . '" /></li>'; } } else { // それ以外のページ $str .= '<li>' . wp_title('', false) . '</li>'; } $str .= '</ul></nav>'; } echo $str; }

↑このコードのうち

} elseif (is_single()) { // 投稿ページ

の前あたりにカスタム投稿タイプを判定する条件分岐を入れるのが良いと思います。

} elseif ( is_post_type_archive( 'event' ) ) { // カスタム投稿タイプのパンくずリストの出力
}

↑このように作成したカスタム投稿タイプのslug(例:event)を判別する条件分岐を入れる必要があります。
パンくずリストの出力はその下の「投稿ページ(is_single())」の出力を参考にするのが良いと思います。ポイントは

$categories = get_the_category($post->ID);

ではカテゴリーは取れないので、

$categories = get_the_terms($post->ID, 'event');

などのようにする必要があります。

メタデスクリプションやタイトルなどもカスタム投稿タイプには対応していません(技術的に不可能だと思われます。自由にアレンジしたい人がカスタム投稿タイプを使用すると思うので、SANGOのフィールドが自動で出力されるのも人によっては迷惑だと思うので、対応する予定もありません)。

よろしくお願いします。

アバター
headcocoro
2019/07/30

何度もすみません。
コードを紹介いただいたとおりに記述してみたのですが、
カスタム投稿タイプ用に書いた(is_singleの内容をコピー)
$cat = $categories[0];
のところでエラーがでてしまいます。
この行を削除するとパンくずの段落は表示されるようになりましたが、
所属ディレクトリーが表示されるはずのところがブランクとなりました。

サルワカくん
サルワカくん
2019/07/31

一度コードを整理したので載せます。

function breadcrumb() { global $post; $str = ''; if (!is_home() && !is_admin()) { // トップページ、管理画面では表示しない $str .= '<nav id="breadcrumb" class="breadcrumb"><ul itemscope itemtype="http://schema.org/BreadcrumbList">'; // ホームのぱんくず $str .= '<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a href="' . home_url() . '" itemprop="item"><span itemprop="name">ホーム</span></a><meta itemprop="position" content="1" /></li>'; if (is_category()) { // カテゴリーページ $cat = get_queried_object(); if ($cat->parent != 0) { $ancestors = array_reverse(get_ancestors($cat->cat_ID, 'category')); $i = 2; foreach ($ancestors as $ancestor) { $str .= '<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a href="' . esc_url(get_category_link($ancestor)) . '" itemprop="item"><span itemprop="name">' . esc_attr(get_cat_name($ancestor)) . '</span></a><meta itemprop="position" content="' . $i . '" /></li>'; $i++; } // end foreach } //END カテゴリー } elseif (is_tag()) { // タグページ $str .= '<li><i class="fa fa-tag"></i> タグ</li>'; } elseif (is_date()) { // 日付ページ if (is_day()) { $str .= '<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a href="' . get_year_link(get_query_var('year')) . '" itemprop="item"><span itemprop="name">' . get_query_var('year') . '年</span></a><meta itemprop="position" content="2" /></li>'; $str .= '<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a href="' . get_month_link(get_query_var('year'), get_query_var('monthnum')) . '" itemprop="item"><span itemprop="name">' . get_query_var('monthnum') . '月</span></a><meta itemprop="position" content="3" /></li>'; } elseif (is_month()) { $str .= '<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a href="' . get_year_link(get_query_var('year')) . '" itemprop="item"><span itemprop="name">' . get_query_var('year') . '年</span></a><meta itemprop="position" content="2" /></li>'; } } elseif (is_author()) { // 著者ページ $str .= '<li>著者</li>'; } elseif (is_page()) { if ($post->post_parent != 0) { $ancestors = array_reverse(get_post_ancestors($post->ID)); $i = 2; foreach ($ancestors as $ancestor) { $str .= '<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a href="' . esc_url(get_permalink($ancestor)) . '" itemprop="item"><span itemprop="name">' . esc_attr(get_the_title($ancestor)) . '</span></a><meta itemprop="position" content="' . $i . '" /></li>'; $i++; } } } elseif (is_singular('event')) { // カスタム投稿タイプ「event」の投稿ページ // イベントのパンくずリストを返す $str .= '<li><a href="★カテゴリーのURL★">イベント</a></li>'; } elseif (is_post_type_archive('event')) { // カスタム投稿タイプ「event」のカテゴリーページ // 「event」のカテゴリーページのパンくずリストはHOMEへのリンクだけ? $str .= ''; } elseif (is_single()) { // 投稿ページ $categories = get_the_category($post->ID); if ($categories) { $cat = $categories[0]; $i = 2; if ($cat->parent != 0) { $ancestors = array_reverse(get_ancestors($cat->cat_ID, 'category')); foreach ($ancestors as $ancestor) { $str .= '<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a href="' . esc_url(get_category_link($ancestor)) . '" itemprop="item"><span itemprop="name">' . esc_attr(get_cat_name($ancestor)) . '</span></a><meta itemprop="position" content="' . $i . '" /></li>'; $i++; } } $str .= '<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a href="' . esc_url(get_category_link($cat->term_id)) . '" itemprop="item"><span itemprop="name">' . esc_attr($cat->cat_name) . '</span></a><meta itemprop="position" content="' . $i . '" /></li>'; } } else { // それ以外のページ $str .= '<li>' . wp_title('', false) . '</li>'; } $str .= '</ul></nav>'; } echo $str; }

「どのようなカスタム投稿タイプを作っているのか」「どのようなカテゴリーが紐づきうるのか」などによってやり方が異なるので、あくまでも一例です。
(ユーザーが自由に作ることができるがゆえにテーマ本体では対応できないのですね)

複数のカテゴリーが紐づきうる場合は、get_the_terms()を使ったカテゴリーの取得が必要ですが、もしカスタム投稿タイプeventのカテゴリーが「イベント」だけなのであれば、パンくずリストの出力を上記のように「イベント」に固定してしまっても良いかと思われます。

そのカテゴリーページ上に表示されるパンくずリストがどうなるかイメージが湧かなかったため、「HOME」だけが出力されるようにしています。お好みで調整してください。

(ちなみにこちらのコード内に含まれるis_singular()is_post_type_archive()などはWordPress標準の関数なので、よく分からない処理があったら調べていただければと思います)