Created
December 13, 2012 08:31
-
-
Save wokamoto/4275006 to your computer and use it in GitHub Desktop.
[WordPress] 添付画像表示ページのタイトルを親投稿のタイトルに合わせる。
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 | |
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