Help us understand the problem. What is going on with this article?

前後のリンクのタイトルを文字数で丸める

More than 5 years have passed since last update.
functions.php
/* 前後リンクを生成(長いタイトルを丸める)
============================================= */

function Custom_previous_post_link($maxlen = -1, $format='« %link', $link='%title', $in_same_cat = false, $excluded_categories = '') {
Custom_adjacent_post_link($maxlen, $format, $link, $in_same_cat, $excluded_categories, true, $maxlen);
}
function Custom_next_post_link($maxlen = -1, $format='%link »', $link='%title', $in_same_cat = false, $excluded_categories = '') {
Custom_adjacent_post_link($maxlen, $format, $link, $in_same_cat, $excluded_categories, false);
}

function Custom_adjacent_post_link($maxlen = -1, $format='« %link', $link='%title', $in_same_cat = false, $excluded_categories = '', $previous = true) {

if ( $previous && is_attachment() )
$post = & get_post($GLOBALS['post']->post_parent);
else
$post = get_adjacent_post($in_same_cat, $excluded_categories, $previous);

if ( !$post )
return;

$tCnt = mb_strlen( $post->post_title, get_bloginfo('charset') );
if(($maxlen > 0)&&($tCnt > $maxlen)) {
$title = mb_substr( $post->post_title, 0, $maxlen, get_bloginfo('charset') ) . '…';
} else {
$title = $post->post_title;
}

if ( empty($post->post_title) )
$title = $previous ? __('Previous Post') : __('Next Post');

$title = apply_filters('the_title', $title, $post->ID);
$date = mysql2date(get_option('date_format'), $post->post_date);
$rel = $previous ? 'prev' : 'next';

$string = '<a href="'.get_permalink($post).'" rel="'.$rel.'">';
$link = str_replace('%title', $title, $link);
$link = str_replace('%date', $date, $link);
$link = $string . $link . '</a>';

$format = str_replace('%link', $link, $format);
echo $format;
}


で該当するprevious(next)_post_link()を探してきて、下記のように変更

//タイトルを丸める場合
<?php Custom_previous_post_link(15) ?>
<?php Custom_next_post_link(15) ?>


//フォーマットなどをすべてのオプションを指定する場合
<?php Custom_previous_post_link(12, '%link', '&laquo; %title', true) ?>
<?php Custom_next_post_link(12, '%link', '&laquo; %title', true) ?>

最初の引数を丸める文字数を指定して、その後の引数は、previous post linkのオプションと同じように指定する。

参考

kimihito_
株式会社ミノタケというひとりぼっちの会社でRailsやらなんやらを書いています。
http://kimihito.hatenablog.com/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした