[WordPress] 添付画像表示ページのタイトルを親投稿のタイトルに合わせる

<?php
function my_attachment_title($title, $id) {
if ( is_admin() )
return $title;
$post = is_object($id)
? $id
: $post = get_post($id);
if ( !$post || $post->post_type !== 'attachment' )
return $title;
if ( $post_parent = get_post($post->post_parent) ) {
$title = intval($post->menu_order) > 0
? $post_parent->post_title . ' (' . intval($post->menu_order) . ')'
: $post_parent->post_title;
$title = apply_filters('the_title', $title, $post_parent->ID);
}
unset($post);
unset($post_parent);
return $title;
}
add_filter('the_title', 'my_attachment_title', 10, 2);
add_filter('single_post_title', 'my_attachment_title', 10, 2);