2022年04月29日

WP関数:get_ancestors()

この関数の説明

WP関数の get_ancestors() は、固定ページIDか、投稿のカテゴリIDを指定して、先祖のID配列を取得する関数です。

 


関数

get_ancestors( $object_id, $object_type, $resource_type )

 


パラメータ

$object_id(整数)(必須)

固定ページIDか、投稿のカテゴリIDを指定します。

 

$object_type(文字列)(必須)

オブジェクトの種類を指定します。

'page'      :固定ページの祖先を取得する時に指定

'category' :投稿のカテゴリの祖先を取得する時に指定

 

$resource_type(文字列) (オプション)

カスタム投稿の祖先IDの配列を取得する時に指定します。初期値: なし
'post_type':カスタム投稿タイプ
'taxonomy': カスタムタクソノミー

 


戻り値

祖先オブジェクトの ID の配列を返します。

配列の並びは階層の下から上の順です。

 


事例

①投稿ページのパンくず処理

<?php if(is_single()):?>
	<?php		
		$icon_home='<i class="fas fa-home"></i> '; // Font Awesomeのホームアイコンの設定
		$icon_cat    ='<i class="far fa-clone"></i> '; // Font Awesomeのカテゴリアイコンの設定
		$icon_file    ='<i class="far fa-file"></i> ';    // Font Awesomeのファイルアイコンの設定

		$title    = get_the_title();// タイトルの取得
		$cat     = get_the_category();// カテゴリ情報の取得
		$cat_id = $cat[0]->cat_ID;// カテゴリIDの取得
		$cat_array = get_ancestors( $cat_id , 'category' ); // 先祖のカテゴリIDをarrayで取得
		$cat_array = array_reverse($cat_array);                // カテゴリIDの順番を逆にする
	?>
	<a href='<?php echo esc_url(home_url()); ?>'><?php echo $icon_home; ?>HOME</a>&nbsp;/&nbsp;<!-- ホームリンクの表示-->
	<?php foreach($cat_array as $cat):?><!-- カテゴリの先祖リンクの表示-->
		<a href='<?php echo esc_url(get_category_link($cat));?>'><?php echo $icon_cat.esc_html(get_cat_name($cat));?></a>&nbsp;/&nbsp;
	<?php endforeach;?>
	<a href='<?php echo esc_url(get_category_link($cat_id));?>'><?php echo $icon_cat.esc_html(get_cat_name($cat_id));?></a>&nbsp;/&nbsp;<!-- 現在カテゴリの表示 -->
	<?php echo $icon_file.esc_html($title);?><!-- 文書名の表示-->
<?php endif;?>

■10行目でカテゴリの先祖を抽出し、11行目でその順番を逆にしています。

■上記は下記にようなパンくずを表示します。

上記はこの投稿のカテゴリが[WordPress関数]で更にこのカテゴリに親があった場合です。

■13~18行目でURLechoする際にエスケープ処理をしています。

 

②固定ページのパンくず処理

<?php if(is_page()):?>
	<?php		
		$icon_home='<i class="fas fa-home"></i> ';  // Font Awesomeのホームアイコンの設定
		$icon_cat    ='<i class="far fa-clone"></i> ';  // Font Awesomeのカテゴリアイコンの設定
		$icon_file    ='<i class="far fa-file"></i> ';     // Font Awesomeのファイルアイコンの設定

		$title  = get_the_title();    // 現在文書のタイトルの取得
		$id     = get_the_ID();      // 固定ページIDの取得
		$page_array = get_ancestors( $id , 'page' );  // 先祖のIDをarrayで取得
		$page_array = array_reverse($page_array);  // カテゴリIDの順番を逆にする
	?>
	<a href='<?php echo esc_url(home_url()); ?>'><?php echo $icon_home;?>HOME</a>&nbsp;/&nbsp;<!-- ホームリンクの表示-->
	<?php foreach($page_array as $page_id):?> <!-- ページの先祖リンクの表示-->
		<a href='<?php echo esc_url(get_permalink($page_id));?>'><?php echo $icon_cat.esc_html(get_the_title($page_id));?></a>&nbsp;/&nbsp;
	<?php endforeach;?>
	<?php echo $icon_file.esc_html($title);?><!-- 文書名の表示-->
<?php endif;?>

■9行目で固定ページの先祖を抽出し、10行目でその順番を逆にしています。

■上記は下記にようなパンくずを表示します。

固定ページ[Wordpress 関数]の親パージで階層1、階層2があった場合です。

■12~16行目でURLをechoする際にエスケープ処理をしています。

 


関連情報

関連関数は カテゴリ関連関数 を参照してください

 

関数一覧
  • 1.WPのエスケープ処理
  • 2.WPプログラムの基本
  • 3.ヘッダ/フッタ/サイドバで使われる関数
  • 4.DBからの読込関数
  • 5.一般テンプレートの中で使われる関数
  • 6.その他のWP関数
  • add_shortcode()
  • bloginfo()
  • body_class()
  • date_i18n()
  • dynamic_sidebar()
  • edit_post_link()
  • esc_attr()
  • esc_html_e()
  • esc_html()
  • esc_url()
  • get_ancestors()
  • get_cat_ID()
  • get_cat_name()
  • get_category_link()
  • get_category_parents()
  • get_category()
  • get_footer()
  • get_header()
  • get_home_url()
  • get_post_format()
  • get_post_meta()
  • get_post_thumbnail_id()
  • get_query_var()
  • get_search_query()
  • get_sidebar()
  • get_site_url()
  • get_stylesheet_directory_uri()
  • get_stylesheet_uri()
  • get_template_directory_uri()
  • get_template_part()
  • get_the_category()
  • get_the_content()
  • get_the_date()
  • get_the_excerpt()
  • get_the_ID()
  • get_the_modified_date()
  • get_the_modified_time()
  • get_the_permalink()
  • get_the_post_thumbnail()
  • get_the_tags()
  • get_the_time()
  • get_the_title()
  • get_uploaded_header_images()
  • get_year_link()
  • has_category()
  • has_custom_logo()
  • has_header_image()
  • has_header_video()
  • has_nav_menu()
  • has_post_thumbnail()
  • has_tag()
  • header_image()
  • home_url()
  • is_admin()
  • is_category()
  • is_date()
  • is_front_page()
  • is_home()
  • is_main_query()
  • is_page()
  • is_search()
  • is_single()
  • language_attributes()
  • the_category()
  • the_content()
  • the_custom_header_markup()
  • the_custom_logo()
  • the_date()
  • the_excerpt()
  • the_ID()
  • the_modified_date()
  • the_modified_time()
  • the_permalink()
  • the_post_thumbnail()
  • the_search_query()
  • the_tags()
  • the_time()
  • the_title()
  • update_post_meta()
  • wp_get_attachment_image_src()
  • wp_nav_menu()
  • WP_Query()