Last active
December 15, 2021 08:01
-
-
Save mypacecreator/0c4671f6494000b008cdb46cd92f7e11 to your computer and use it in GitHub Desktop.
meta, title関連でよく頼まれるやつ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//og:titleにサイト名を入れたいと言われた時用 | |
function custom_open_graph_title( $og_tags ) { | |
if( is_home() ){ | |
$og_tags['og:title'] = 'ブログ|' . get_bloginfo( 'name' ); | |
} elseif ( !is_front_page() ){ | |
$og_tags['og:title'] .= '|' . get_bloginfo( 'name' ); | |
} | |
return $og_tags; | |
} | |
add_filter( 'wp_sitemanager_open_graph_tags', 'custom_open_graph_title' ); | |
//タイトルタグのセパレータを’ | ‘に | |
function mypace_custom_title_separator( $sep ){ | |
$sep = ' | '; | |
return $sep; | |
} | |
add_filter( 'document_title_separator', 'mypace_custom_title_separator' ); | |
//タイトルタグに投稿タイプ名とか入れる | |
if ( ! function_exists( 'mypace_custom_title' ) ) { | |
function mypace_custom_title( $title ){ | |
if( get_post_meta( get_the_ID(), 'mypace_title_tag', true ) ){ | |
return; //mypace Custom Title Tag プラグイン使用時は以下の処理をスルー | |
} | |
$post_type_name = NULL; | |
if( is_singular() && !is_singular( 'post' ) && !is_page() ){ //タイトルタグカスタマイズの範囲を条件分岐で指定 | |
$post_type_slug = get_post_type(); //投稿タイプスラッグを取得 | |
$post_type_obj = get_post_type_object( $post_type_slug ); //投稿タイプの情報をオブジェクトで取得 | |
$post_type_name = $post_type_obj->labels->singular_name; | |
} elseif ( is_tax() ) { | |
$tax_slug = get_query_var( 'taxonomy' ); //今見てるアーカイブページのタクソノミー名を取得 | |
$tax_obj = get_taxonomy( $tax_slug ); | |
$post_type_slug = $tax_obj->object_type[0]; //タクソノミーに紐付いている投稿タイプのスラッグを取得 | |
$post_type_obj = get_post_type_object( $post_type_slug ); //投稿タイプの情報をオブジェクトで取得 | |
$post_type_name = $post_type_obj->labels->singular_name; | |
} elseif( is_singular( 'post' ) ){ | |
$single_cat = get_the_category(); | |
$post_type_name = $single_cat[0]->cat_name; | |
} | |
if( isset( $post_type_name ) ){ | |
$title['title'] .= ' | ' . $post_type_name; //ページタイトルに追加 | |
} | |
return $title; //条件外の時はWordPressコアで定義されているタイトルのまま出力 | |
} | |
} | |
add_filter( 'document_title_parts', 'mypace_custom_title' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment