/
short_title_link.php
executable file
·123 lines (95 loc) · 2.92 KB
/
short_title_link.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/*
Plugin Name: Short Title Link
Plugin URI: http://blog.lunatic-code.net/wp_plugin/
Description: Truncate navigation link
Author: Masaki Komagata <komagata@fjord.jp>
Version: 0.3
Author URI: http://fjord.jp/
*/
/*
Original: http://blog.lunatic-code.net/webdesign/php/28/
USAGE
<?php previous_post_link_short() ?>
<?php next_post_link_short() ?>
*/
// Creating short title
function create_short_title($title_short,$length){
$ls_search = array( """, // "
""", // "
""", // "
"<", // <
"<", // <
"<", // <
">", // >
">", // >
">", // >
"–", // -
"&", // &
"&", // &
"&"); // &
$ls_replace = array( '"', // "
'"', // "
'"', // "
'<', // <"
'<', // <"
'<', // <"
'>', // >"
'>', // >"
'>', // >"
"-", // –-
'&', // &"
'&', // &"
'&'); // &"
$ls_research = array( '&', // &"
'"', // "
"– ", // -
'<', // <"
'>'); // >"
$ls_rereplace = array( "&", // &
"– ", // -
""", // "
"<", // <
">"); // >
$new_title = str_replace($ls_search, $ls_replace, $title_short);
$len = mb_strwidth($new_title); // バイト数を調べる
if( $len>$length ){ //
$title_short = mb_strimwidth($new_title , 0, $length, "...");
$new_title_short = $title_short;
}
else{
$new_title_short = $new_title ;
}
$short_title = str_replace($ls_research, $ls_rereplace, $new_title_short);
return $short_title;
}
// Prev
function previous_post_link_short($format='« %link', $link='%title', $in_same_cat = false, $excluded_categories = '', $length = 32) {
if ( is_attachment() )
$post = & get_post($GLOBALS['post']->post_parent);
else
$post = get_previous_post($in_same_cat, $excluded_categories);
if ( !$post )
return;
$title = apply_filters('the_title', $post->post_title, $post);
$short_title = create_short_title($title,$length);
$string = '<a href="'.get_permalink($post->ID).'"' . ' title="' . $title . '">';
$link = str_replace('%title', $short_title, $link);
$link = $pre . $string . $link . '</a>';
$format = str_replace('%link', $link, $format);
echo $format;
}
// Next
function next_post_link_short($format='%link »', $link='%title', $in_same_cat = false, $excluded_categories = '', $length = 32) {
$post = get_next_post($in_same_cat, $excluded_categories);
if ( !$post )
return;
$title = apply_filters('the_title', $post->post_title, $post);
$short_title = create_short_title($title,$length);
$string = '<a href="'.get_permalink($post->ID).'"' . ' title="' . $title . '">';
$link = str_replace('%title', $short_title, $link);
$link = $string . $link . '</a>';
$format = str_replace('%link', $link, $format);
echo $format;
}
?>