ビジネス向けWEBサイトをつくる

WordPressのカスタム投稿をfunctions.phpで設定する

本ページはプロモーションが含まれています

現在はサイト型CMSと言えるWordPressですが、古くはブログ機能がメインでした。

そのため、投稿機能はドメイン直下のURLとなります。

カテゴリーで、そのページをグループ分けすることも可能ですが、同じコンテンツグループの中にあるのは好ましくないものもあります。

そうした場合、カスタム投稿機能を使います。

カスタム投稿は投稿ページと固定ページの中間のようなページです。

独自の投稿グループを作ることができます。

例えばポートフォリオ、製品、イベント情報等、同じグループにコンテンツが複数存在するものがあげられます。

では、カスタム投稿はどのように作るのでしょうか?

このページでは、functions.phpを設定する方法をメインに紹介します。

数多くのブログでも紹介されている内容ですが、当ページでは特にブロックエディターに対応するための設定やラベル設定も実例で紹介しますので、そのままコピーして使えるはずです。

functions.phpの設定以外にも、プラグインでカスタム投稿を作れるものがあるので、合わせてご紹介しましょう。

しかし、最も面倒なのはパンくずリストの対応になるでしょう。

パンくずリストについては以下のページが参考になると思います。

すみません。上記で紹介したページのコードは検証していません。構造化に対応するためにはもっと複雑になります。
当方では構造化に対応した関数を使っていますが、カスタマイズを重ねたので、汎用的なものとして紹介する自信がありません。
パンくずリストの構造化はAll in One SEOのJSON LDを使ってもいいかもしれません。

念のため私が使っているパンくずリストの関数をページ末に添付しておきます。実際に動作していますが、コピーペーストで動くかどうかは分かりません。

【PR】「Theme3」では、企業サイトのWordPressテーマに「 ワードプレステーマTCD 」をオススメしています。
WordPressテーマ「GENESIS」の詳細をTCDのページで見る

ブロックエディタ対応
カスタム投稿タイプ「お知らせ」「サービス(事業案内)」
SEO機能(metaタグ、OGP、高速化)
他、機能多数。

ご購入は以下のバナーリンクページの購入ボタンをクリックしてください。

お知らせのカスタム投稿を設定する

カスタム投稿を使用するシーンで最もよくあるのはお知らせやニュースの投稿とブログ投稿を分けたいときです。

ときどき「お知らせとコラム」といったナビを見かけます。
しかし、そもそもコラムとお知らせは趣旨が大きく異なります。

これらはグループ分けすべきです。カテゴリー分けは同一グループを分けるものなので、適さないでしょう。

こんな場合に使用するのが「カスタム投稿」です。

カスタム投稿を作るのは簡単です。

以下のコードをfunctions.phpに追加します。(日本語対応のみのコードです。)

/* ---------------------------------------------------------- *///
/* カスタム投稿タイプ追加
---------------------------------------------------------- *///

//お知らせ
function create_post_type_news() {
	$news_bread = "お知らせ" ;

	$labels = array(
		'name'	=> $news_bread,
		'all_items'	=> $news_bread . '一覧',
		'add_new' => $news_bread . 'を追加',
		'search_items' => $news_bread . 'を検索',
		'singular_name' => $news_bread,
		'menu_name' => $news_bread,
		'edit_item' => $news_bread,
	);
	$args = array(
		'labels'	=> $labels,
		'supports'	=> array('title','editor','excerpt','thumbnail'),
		'public'	=> true,	// 公開するかどうか
		'show_ui'	=> true,	// メニューに表示するかどうか
		'menu_position'	=> 4,	// メニューの表示位置
		'has_archive'	=> true,	// アーカイブページの作成
		'hierarchical' => true,
		'show_in_rest' => true,
	);
	register_post_type( 'news', $args );
	
///カテゴリータイプ
	register_taxonomy(
		'news-cat',  // カスタム分類名
		'news',      // カスタム分類を使用する投稿タイプ名
		array(
			'hierarchical' => true,
			'label' => 'カテゴリー',
			'singular_label' => 'カテゴリー',
			'public' => true,
			'show_admin_column' => true, //管理画面のカスタム投稿一覧にカテゴリを表示
			'show_ui' => true,
			'rewrite' => array( 'slug' => 'news-cat' ),
			'show_in_rest' => true,
		)
	);

///タグタイプ
	register_taxonomy(
		'news-tag',  // カスタム分類名
		'news',      // カスタム分類を使用する投稿タイプ名
		array(
			'hierarchical' => false,
			'label' => 'タグ',
			'singular_label' => 'タグ',
			'public' => true,
			'show_admin_column' => true, //管理画面のカスタム投稿一覧にタグを表示
			'show_ui' => true,
			'rewrite' => array( 'slug' => 'news-tag' ),
			'show_in_rest' => true,
		)
	);
}
add_action( 'init', 'create_post_type_news', 0 );

タグタイプの設定が必要無い場合はregister_taxonomy(〜);を削除します。

私は、お知らせにタグを設定することはあまりありません。

ポイントは以下です。

labelsパラメーター

「labels」には、たくさんパラメーターがあります。

以下のページに詳しいです。合わせてお読みください。

ブロックエディタに対応

ブロックエディタに対応させるためには‘show_in_rest’ => trueにします。

管理画面

管理画面は以下の様になります。

「投稿」の下に「お知らせ」が表示されています。

また、「お知らせを追加」や「お知らせを検索」ボタンも正しく表示されています。

【PR】「Theme3」では、企業サイトのWordPressテーマに「 ワードプレステーマTCD 」をオススメしています。
WordPressテーマ「SOLARIS」の詳細をTCDのページで見る

レスポンシブ対応
カスタム投稿タイプ「お知らせ」「企業情報」「サービス」「プロジェクト」
高速化設定(絵文字読み込み・コード最適化)
他、機能多数。

ご購入は以下のバナーリンクページの購入ボタンをクリックしてください。

カスタム投稿タイプのテンプレートphpを追加

お知らせカスタム投稿を通常投稿と全く同じように表示する場合は、以上のfunctions.phpの設定だけで問題無いでしょう。

しかし、お知らせのページやアーカイブページを独自のものにしたい場合は以下のカスタムテンプレートを追加します。

テンプレート名はカスタム投稿タイプ名(news)やカスタム分類名(news-cat、news-tag)を付加して作ります。

親テーマに通常投稿のテンプレートがある場合はコピーして書き換えます。

テンプレート説明
single-newsお知らせページを作成します。
content-news.php場合によってはページ内容を変更します。
※テーマファイルではなくテンプレートパーツです。
archive-news.phpお知らせのアーカイブページを作成します。
taxonomy-news-catお知らせのカテゴリーアーカイブページを作成します。
taxonomy-news-tagお知らせのタグアーカイブページを作成します。

つぎに、プラグインによってカスタム投稿を追加する方法を紹介します。

【PR】「Theme3」では、企業サイトのWordPressテーマに「 ワードプレステーマTCD 」をオススメしています。
WordPressテーマ「GENESIS」の詳細をTCDのページで見る

ブロックエディタ対応
カスタム投稿タイプ「お知らせ」「サービス(事業案内)」
SEO機能(metaタグ、OGP、高速化)
他、機能多数。

ご購入は以下のバナーリンクページの購入ボタンをクリックしてください。

カスタム投稿作成プラグイン

カスタム投稿を追加するプラグインには以下のようなものがあります。

Custom Post Type UI

「Custom Post Type UI」がよく使われているようです。(私はプラグインをあまり使いません。)

「Custom Post Type UI」については、以下のページが詳しいです。

VK All in One Expansion Unit

テーマ「lightning」を使ったときに知ったのは「VK All in One Expansion Unit」です。

このプラグインは、カスタム投稿を設定できます。

以下のページで解説しました。

まとめ

カスタム投稿を使えば、様々なグループの投稿が手軽に作れるようになります。

WordPressをサイトCMSとして使い場合は必須の機能です。

ブログだけだとWordPressの機能は半減します。

カスタム投稿を使って様々なグループをデータベース化すれば、あらゆるサイトコンテンツをメンテナンス性豊かに表現出来るはずです。

WordPressを使うなら、カスタム投稿を使い倒しましょう。

付録(構造化対応パンくずリスト関数)

カスタム投稿のパンくずリストは「Home」の下位になるので複雑化します。

コードには以下の注意があります。

  • 使用するテーマのカスタムなCSSを定義しています。
  • 「itemprop=”position” content=」は計算した方が良いと思います。
  • もっと効率化できる気がしています。
  • ひょっとするとバグがあるかも(実際の動作では見つかっていませんが…)
  • isotypeテーマに搭載されていた関数をカスタマイズしたものです。
//---------------------------------------------------------- */
/* Breadcrumb @http://blog.uklab.jp/web/wordpress-breadcrumbs-list-no-plugin/
---------------------------------------------------------- */
function get_breadcrumb_list2($include_category = 1, $include_tag = 1, $include_taxonomy = 1)
{
	// Base breadcrumbs
	$base_breadcrumb = '<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a href="'.get_home_url().'" itemprop="item"><span itemprop="name">HOME</span></a><meta itemprop="position" content="1"></li>';
	$top_url = get_home_url(null,'/');

	// Loop outside support
	global $query_string;
	global $post;
	query_posts($query_string);
	if (have_posts()) : while(have_posts()) : the_post();
	endwhile; endif;
	// Query reset
	wp_reset_query();

	// Get the number of pages (to be changed to 1 because it is the first page if the number of pages (of 0))
	if(get_query_var('paged') == 0): $page = 1; else: $page = get_query_var('paged') ; endif;

	// For non-post-fixed page and attachment page
	if(is_singular() && !is_attachment())
	{
		// Get all of the categories that is attached to the article
		$categories = get_the_category();
		// Run to when there is a category
		if(!empty($categories))
		{
			$category_count = count($categories);
			$loop = 1;
			$category_list = '';
			foreach($categories as $category)
			{
				// When was the value is 1, and is stored in a variable, including a taxonomy of categories in the URL.
				if($include_category === 1)
				{
					$category_list .= '<a href="'.esc_url(get_term_link($category)).'" itemprop="item"><span itemprop="name">'.esc_html($category->name).'</span></a>';
}
				// If you wanted other than 1 if the value is not specified, and is stored in a variable and not include the taxonomy of categories in the URL.
				else
				{
					$category_list .= '<a href="'.$top_url.esc_html($category->slug).'/" itemprop="item"><span itemprop="name">'.esc_html($category->name).'</span></a>';
				}
				// Slash added only if it is not the end of the loop (more categories corresponding)
				if($loop != $category_count)
				{
					$category_list .= ' / ';
				}
				++$loop;
			}
		}
		// Store a NULL if there is no category
		else
		{
			$category_list = null;
		}

		// I get all the tags attached to the article
		$tags = get_the_tags();

		// Get all the taxonomy
		$taxonomies = get_the_taxonomies();
		// Run to when there is a taxonomy
		if(!empty($taxonomies))
		{
			$term_list = '';
			$taxonomies_count = count($taxonomies);
			$taxonomies_loop = 1;
			foreach(array_keys($taxonomies) as $key)
			{
				// Get term
				$terms = get_the_terms($post->ID, $key);
				$terms_count = count($terms);
				$loop = 1;
				foreach($terms as $term)
				{
					// Value it was 1 and stored in the variable, including a taxonomy in the URL.
					if($include_taxonomy === 1)
					{
						$term_list .= '<a href="'.$top_url.esc_html($term->taxonomy).'/'.esc_html($term->slug).'/" itemprop="item"><span itemprop="name">'.esc_html($term->name).'</span></a>';
					}
					// To be stored in the variable without including the taxonomy in the URL if you wanted other than 1 or not is specified value.
					else
					{
						$term_list .= '<a href="'.$top_url.esc_html($term->slug).'/" itemprop="item"><span itemprop="name">'.esc_html($term->name).'</span></a>';
					}
					// Slash added only if it is not the end of the loop (s term support)
					if($loop != $terms_count)
					{
						$term_list .= ' / ';
					}
					++$loop;
				}
				// Slash added only if it is not the end of the loop (multiple taxonomies corresponding)
				if($taxonomies_loop != $taxonomies_count)
				{
					$term_list .= ' / ';
				}
				++$taxonomies_loop;
			}
		}
		// Store a NULL if there is no taxonomy
		else
		{
			$term_list = null;
		}
	}

	// It stores the basic breadcrumbs to a variable.
	$breadcrumb_lists = $base_breadcrumb;

	// For Home
	if(is_home())
	{
		// You want to override the basic breadcrumbs
		$breadcrumb_lists = '<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a href="'.get_home_url().'" itemprop="item"><span itemprop="name">HOME</span></a><meta itemprop="position" content="1"></li><div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><h1><span itemprop="name">コラム</span></h1><meta itemprop="position" content="2"></li>';
	}
	// In the case of custom post type archive : カスタム投稿アーカイブ
	elseif( is_post_type_archive( array( 'news' ) ) )
	{
		// I store the number of pages is added if greater than 1 the (* page)
		if($page > 1)
		{
			$breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><h1><span itemprop="name">'.esc_html(get_post_type_object( get_post_type() )->label).'('.$page.'Page)</span></h1><meta itemprop="position" content="2"></li>';
		}
		// If the number of pages is 1 to be stored in (* page) story
		else
		{
			$breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><h1><span itemprop="name">'.esc_html(get_post_type_object( get_post_type() )->label).'</span></h1><meta itemprop="position" content="2"></li>';
		}
	}
	//カスタム投稿ページ
	elseif(is_singular( array( 'news' ) ))
	{
		//親ページの有無を判定
		if($post->post_parent == 0){ 
		//親ページ無し
		$breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a href="'.get_post_type_archive_link( get_post_type() ).'" itemprop="item"><span itemprop="name">'.esc_html(get_post_type_object( get_post_type() )->label).'</span></a><meta itemprop="position" content="2"></li><div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><h1><span itemprop="name">'.esc_html(get_the_title()).'</span></h1><meta itemprop="position" content="3"></li>'; 
		} else {
		//親ページあり
		$breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a href="'.get_post_type_archive_link( get_post_type() ).'" itemprop="item"><span itemprop="name">'.esc_html(get_post_type_object( get_post_type() )->label).'</span></a><meta itemprop="position" content="2"></li><div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a href="'.get_permalink($post->post_parent).'" itemprop="item"><span itemprop="name">'.esc_html(get_the_title($post->post_parent)).'</span></a><meta itemprop="position" content="3"></li><div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><h1><span itemprop="name">'.esc_html(get_the_title()).'</span></h1><meta itemprop="position" content="4"></li>';}
	}
	// For archive of non-custom post type
	elseif(is_archive())
	{
		
		//最新情報の前に空のliが出来る		
		//$breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">'.$category_list.'</li>';
				
		// In the case of year archive
		if(is_year())
		{
			//最新情報を加える
				$breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a href="'.get_post_type_archive_link(get_post_type_object(get_post_type())->name).'" itemprop="item"><span itemprop="name">'.esc_html(get_post_type_object(get_post_type())->labels->singular_name).'</span></a><meta itemprop="position" content="2"></li>';
				
			// I store the number of pages is added if greater than 1 the (* page)
			if($page > 1)
			{
				$breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><h1><span itemprop="name">'.esc_html(get_the_time("Y年")).__( 'List of articles', 'mysticblue' ).'('.$page.'Page)</span></h1><meta itemprop="position" content="3"></li>';
			}
			// If the number of pages is 1 to be stored in (* page) story
			else
			{
				$breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><h1><span itemprop="name">'.esc_html(get_the_time("Y年")).__( 'List of articles', 'mysticblue' ).'</span></h1><meta itemprop="position" content="3"></li>';
			}
		}
		// In the case of the month archive
		elseif(is_month())
		{
			//最新情報を加える
				$breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a href="'.get_post_type_archive_link(get_post_type_object(get_post_type())->name).'" itemprop="item"><span itemprop="name">'.esc_html(get_post_type_object(get_post_type())->labels->singular_name).'</span></a><meta itemprop="position" content="2"></li>';
				
			// I store the number of pages is added if greater than 1 the (* page)
			if($page > 1)
			{
				$breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><h1><span itemprop="name">'.esc_html(get_the_time("Y年m月")).__( 'List of articles', 'mysticblue' ).'('.$page.'Page)</span></h1><meta itemprop="position" content="3"></li>';
			}
			// If the number of pages is 1 to be stored in (* page) story
			else
			{
				$breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><h1><span itemprop="name">'.esc_html(get_the_time("Y年m月")).__( 'List of articles', 'mysticblue' ).'</span></h1><meta itemprop="position" content="3"></li>';
			}
		}
		// In the case of the day archive
		elseif(is_day())
		{
			//最新情報を加える
				$breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a href="'.get_post_type_archive_link(get_post_type_object(get_post_type())->name).'" itemprop="item"><span itemprop="name">'.esc_html(get_post_type_object(get_post_type())->labels->singular_name).'</span></a><meta itemprop="position" content="2"></li>';
				
			// I store the number of pages is added if greater than 1 the (* page)
			if($page > 1)
			{
				$breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><h1><span itemprop="name">'.esc_html(get_the_time("Y年m月d日")).__( 'List of articles', 'mysticblue' ).'('.$page.'Page)</span></h1><meta itemprop="position" content="3"></li>';
			}
			// If the number of pages is 1 to be stored in (* page) story
			else
			{
				$breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><h1><span itemprop="name">'.esc_html(get_the_time("Y年m月d日")).__( 'List of articles', 'mysticblue' ).'</span></h1><meta itemprop="position" content="3"></li>';
			}
		}
		// In the case of category
		elseif(is_category())
		{
			//最新情報を加える
				$breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a href="'.get_post_type_archive_link(get_post_type_object(get_post_type())->name).'" itemprop="item"><span itemprop="name">'.esc_html(get_post_type_object(get_post_type())->labels->singular_name).'</span></a><meta itemprop="position" content="2"></li>';
				
			// I store the number of pages is added if greater than 1 the (* page)
			if($page > 1)
			{
				$breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><h1><span itemprop="name">'.esc_html(single_cat_title('',false)).__( 'List of articles', 'mysticblue' ).'('.$page.'Page)</span></h1><meta itemprop="position" content="3"></li>';
			}
			// If the number of pages is 1 to be stored in (* page) story
			else
			{
				$breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><h1><span itemprop="name">'.esc_html(single_cat_title('',false)).__( 'List of articles', 'mysticblue' ).'</span></h1><meta itemprop="position" content="3"></li>';
				
				//$breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><h1>'.esc_html(single_cat_title('',false)).'</h1></li>';
			}
		}
		// In the case of tag
		elseif(is_tag())
		{
			//最新情報を加える
				$breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a href="'.get_post_type_archive_link(get_post_type_object(get_post_type())->name).'" itemprop="item"><span itemprop="name">'.esc_html(get_post_type_object(get_post_type())->labels->singular_name).'</span></a><meta itemprop="position" content="2"></li>';
				
			// I store the number of pages is added if greater than 1 the (* page)
			if($page > 1)
			{
				$breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><h1><span itemprop="name">'.esc_html(single_tag_title('',false)).__( 'List of articles', 'mysticblue' ).'('.$page.'Page)</span></h1><meta itemprop="position" content="3"></li>';
			}
			// If the number of pages is 1 to be stored in (* page) story
			else
			{
				$breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><h1><span itemprop="name">'.esc_html(single_tag_title('',false)).__( 'List of articles', 'mysticblue' ).'</span></h1><meta itemprop="position" content="3"></li>';
			}
		}
		// If the taxonomy of (custom classification)
		elseif(is_tax())
		{
			//最新情報を加える
				$breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a href="'.get_post_type_archive_link(get_post_type_object(get_post_type())->name).'" itemprop="item"><span itemprop="name">'.esc_html(get_post_type_object(get_post_type())->labels->singular_name).'</span></a><meta itemprop="position" content="2"></li>';
				
			// I store the number of pages is added if greater than 1 the (* page)
			if($page > 1)
			{
				$breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><h1><span itemprop="name">'.esc_html(single_term_title('',false)).__( 'List of articles', 'mysticblue' ).'('.$page.'Page)</span></h1><meta itemprop="position" content="3"></li>';
			}
			// If the number of pages is 1 to be stored in (* page) story
			else
			{
				$breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><h1><span itemprop="name">'.esc_html(single_term_title('',false)).__( 'List of articles', 'mysticblue' ).'</span></h1><meta itemprop="position" content="3"></li>';
			}
		}
		// 最新情報 アーカイブ
		else {
			// I store the number of pages is added if greater than 1 the (* page)
			if($page > 1)
			{
				$breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><h1><span itemprop="name">'.esc_html(get_post_type_object(get_post_type())->labels->singular_name).'('.$page.'Page)</span></h1><meta itemprop="position" content="2"></li>';
			}
			// If the number of pages is 1 to be stored in (* page) story
			else
			{
				$breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><h1><span itemprop="name">'.esc_html(get_post_type_object(get_post_type())->labels->singular_name).'</span></h1><meta itemprop="position" content="2"></li>';
			}
		}
	}
	// Posts page
	elseif(is_single())
	{
		// Get the number of pages (to be changed to 1 because it is the first page if the number of pages (of 0))
		if(get_query_var('page') == 0): $page = 1; else: $page = get_query_var('page') ; endif;
		// In the case of the normal post
		if(get_post_type() === 'post')
		{
			// Get the value of the custom field
			$seo_title = esc_html(get_post_meta($post->ID, 'seo_title', true));

			// Added only if there is a category カテゴリーを最新情報に変更
			if(!empty($category_list))
			{
				//最新情報を加える
				$breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a href="'.get_post_type_archive_link(get_post_type_object(get_post_type())->name).'" itemprop="item"><span itemprop="name">'.esc_html(get_post_type_object(get_post_type())->labels->singular_name).'</span></a><meta itemprop="position" content="2"></li>';
				
				$breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">'.$category_list.'<meta itemprop="position" content="3"></li>';
			}
			// 投稿ページの分割 ? <meta itemprop="position" content="4">
			// In the case of the 2page or later
			if($page > 1)
			{
				// It stores the value if there is value in the custom field
				if(!empty($seo_title))
				{
					$breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><h1><span itemprop="name">'.$seo_title.'</span></h1><meta itemprop="position" content="4"></li>';
				}
				// If there is no value in the custom field
				else
				{
					// Title acquisition when the page is divided
					if(function_exists('get_current_split_string'))
					{
						$split_title = esc_html(get_current_split_string($page));
					}
					else
					{
						$split_title = null;
					}

					// Make it when there is a title in which the page is divided
					if(!empty($split_title))
					{
						$breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><h1><span itemprop="name">'.esc_html(get_the_title()).'【'.$split_title.'】</span></h1><meta itemprop="position" content="4"></li>';
					}
					// Otherwise, it will add the (○ page)
					else
					{
						$breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><h1><span itemprop="name">'.esc_html(get_the_title()).'('.$page.'Page)</span></h1><meta itemprop="position" content="4"></li>';
					}
				}
			}
			// 投稿ページ
			// n the case of the 1page
			else
			{
				// It stores the value if there is value in the custom field
				if(!empty($seo_title))
				{
					$breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><h1><span itemprop="name">'.$seo_title.'</span></h1><meta itemprop="position" content="4"></li>';
				}
				// If there is no value in the custom field
				else
				{
					$breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><h1><span itemprop="name">'.esc_html(get_the_title()).'</span></h1><meta itemprop="position" content="4"></li>';
				}
			}
		}
		// 画像のみ
		// In the case of custom post
		else
		{
			// Added only if there is a term
			if(!empty($term_list))
			{
				$breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><h1><span itemprop="name">'.$term_list.'</span></h1><meta itemprop="position" content="2"></li>';
			}
			$breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><h1><span itemprop="name">'.esc_html(get_the_title()).'</span></h1><meta itemprop="position" content="2"></li>';
		}
		
	}
	// Fixed page
	elseif(is_page()&& $post -> post_parent != 0 ) 
		{
		
$item_position = 2;
      $ancestors = array_reverse( $post->ancestors ); // 投稿の祖先ページの ID を配列として取得
      foreach($ancestors as $ancestor){ // 配列を一覧として表示
        $breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a href="'.get_permalink($ancestor).'" itemprop="item"><span itemprop="name">'.get_the_title($ancestor).'</span></a><meta itemprop="position" content="'.$item_position.'"></li>'; // 親ページの URL とタイトルを表示
        $item_position++;
      }
   
    $breadcrumb_lists .='<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><h1><span itemprop="name">'.esc_html(get_the_title()).'</span></h1><meta itemprop="position" content="'.$item_position.'"></li>'; // 現在の投稿のタイトル
 

	}
	elseif(is_page() ) 
		{
		// ホームを判定
		if(!is_front_page()) {
		$breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><h1><span itemprop="name">'.esc_html(get_the_title()).'</span></h1><meta itemprop="position" content="2"></li>';
		}
	}
	// Search result
	elseif(is_search())
	{
		// I store the number of pages is added if greater than 1 the (○ page)
		if($page > 1)
		{
			$breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><h1><span itemprop="name">「'.esc_html(get_search_query()).'」の検索結果('.$page.'Page)</span></h1><meta itemprop="position" content="2"></li>';
		}
		// If the number of pages is 1 to be stored in (○ page) story
		else
		{
			$breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><h1><span itemprop="name">「'.esc_html(get_search_query()).'」の検索結果</span></h1><meta itemprop="position" content="2"></li>';
		}
	}
	// In the case of page 404
	elseif(is_404())
	{
		$breadcrumb_lists .= '<div class="icon-basics-07"></div><li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><h1>お探しのページは見つかりませんでした</h1><meta itemprop="position" content="2"></li>';
	}
	else
	{
		$breadcrumb_lists = $base_breadcrumb;
	}

	// Breadcrumbs molding
	if(!empty($breadcrumb_lists))
	{
		$breadcrumb_lists = '<div id="breadcrumb"><ul id="breadcrumb_list" itemscope itemtype="http://schema.org/BreadcrumbList">'.$breadcrumb_lists.'</ul></div>';
	}

	return $breadcrumb_lists;
}

【PR】「Theme3」では、企業サイトのWordPressテーマに「 ワードプレステーマTCD 」をオススメしています。
WordPressテーマ「GENESIS」の詳細をTCDのページで見る

ブロックエディタ対応
カスタム投稿タイプ「お知らせ」「サービス(事業案内)」
SEO機能(metaタグ、OGP、高速化)
他、機能多数。

ご購入は以下のバナーリンクページの購入ボタンをクリックしてください。

プロモーション

戦略的WEBサイト構築方法

戦略的WEBサイト構築方法
WEB担当者にオススメ

WEBサイトをビジネス戦略のPDCAサイクルに組み込むための考え方と、サイトを内製化する方法を分かりやすく解説します。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

3 × two =

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)

ABOUT US
lin記事を書いている人
はじめまして。「lin」です。クリエイティブディレクター兼グラフィックデザイナーとして活動しています。おかげさまで、キャリア25年以上になりました。「Theme3」は、私が企画デザイン事務所スラッシュディーの仕事で得たノウハウを公開します。
※以下は私が活動している企業情報にリンクしています。