Yoast SEO を利用していてプラグイン側で提供されている設定以外で条件に応じてタイトルやディスクリプションの出力内容をカスタマイズする場合は Yoast SEO で用意されているフィルターフックの “wpseo_title” や “wpseo_metadesc” を利用します。
Yoast SEO:タイトル&ディスクリプション設定
/**
 * Yoast SEO:タイトル&ディスクリプション設定のカスタマイズ
 */
// タイトル&ディスクリプションなどメタ情報取得関数
function custom_get_cpt_meta()
{
  $meta               = [];
  $title                 = get_the_title();
  $current_pgae = get_query_var('paged') ? (int)get_query_var('paged') : 1; 
  // 
  if ( $current_pgae>1 ) {
    $pnum_prefix = $current_pgae.'ページ目 | ';
  } else {
    $pnum_prefix = '';
  }
  // カスタム投稿タイプが blog の場合
  if ( is_post_type_archive('blog') ) {
    $meta = [
      'title'             => $pnum_prefix.'ブログアーカイブのタイトル',
      'description' => $pnum_prefix.'ブログアーカイブのディスクリプション'
    ];
  }
  // meta情報
	return $meta;
}
// タイトル設定
function custom_wpseo_title($title)
{ 
  if ( is_post_type_archive( 'blog' ) ) {
    $meta  = custom_get_cpt_meta();
    $title = $meta['title'];
} 
  
  // 
  return $title; 
} 
add_filter('wpseo_title', 'custom_wpseo_title');
// ディスクリプション設定
function custom_wpseo_description($description)
{ 
  if ( is_post_type_archive( 'blog' ) ) {
    $meta        = custom_get_cpt_meta();
    $description = $meta['description'];
  } 
  
  // 
  return $description; 
} 
add_filter( 'wpseo_metadesc', 'custom_wpseo_description' );
Yoast SEO フィルターフック
タイトル – wpseo_title
Yoast SEO Titles - API documentation Yoast SEO Titles - API documentation | Yoast developer portalInstructions on how to modify our title values programmatically.
ディスクリプション – wpseo_metadesc
Yoast SEO Meta descriptions: API documentation Yoast SEO Meta descriptions: API documentation | Yoast developer portalInstructions on how to modify our description values programmatically.
動作環境情報
"エックスサーバー" スタンダード(旧X10) "PHP" 8.0.25 "MariaDB" 10.5 "WordPress" 6.2

  
  
  
  

コメント