Skip to content

Instantly share code, notes, and snippets.

@wokamoto
Created December 13, 2012 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wokamoto/4275006 to your computer and use it in GitHub Desktop.
Save wokamoto/4275006 to your computer and use it in GitHub Desktop.
[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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment