我需要在WordPress上自定义previous_post_link()
和next_post_link()
。
例如,我想将“你需要学习的前5种编程语言”等固定链接缩小为“前5种编程......”。
负责创建位于isadjacent_post_link()
wp-includes/link-template.php
的函数
答案 0 :(得分:2)
要为帖子创建自定义相邻链接,我可以使用过滤器钩子next_post_link和previos_post_link;
在functions.php中:
function shrink_previous_post_link($format, $link){
$in_same_cat = false;
$excluded_categories = '';
$previous = true;
$link='%title';
$format='« %link';
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;
$title = $post->post_title;
if ( empty($post->post_title) )
$title = $previous ? __('Previous Post') : __('Next Post');
$rel = $previous ? 'prev' : 'next';
//Save the original title
$original_title = $title;
//create short title, if needed
if (strlen($title)>40){
$first_part = substr($title, 0, 23);
$last_part = substr($title, -17);
$title = $first_part."...".$last_part;
}
$string = '<a href="'.get_permalink($post).'" rel="'.$rel.'" title="'.$original_title.'">';
$link = str_replace('%title', $title, $link);
$link = $string . $link . '</a>';
$format = str_replace('%link', $link, $format);
echo $format;
}
function shrink_next_post_link($format, $link){
$in_same_cat = false;
$excluded_categories = '';
$previous = false;
$link='%title';
$format='%link »';
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;
$title = $post->post_title;
if ( empty($post->post_title) )
$title = $previous ? __('Previous Post') : __('Next Post');
$rel = $previous ? 'prev' : 'next';
//Save the original title
$original_title = $title;
//create short title, if needed
if (strlen($title)>40){
$first_part = substr($title, 0, 23);
$last_part = substr($title, -17);
$title = $first_part."...".$last_part;
}
$string = '<a href="'.get_permalink($post).'" rel="'.$rel.'" title="'.$original_title.'">';
$link = str_replace('%title', $title, $link);
$link = $string . $link . '</a>';
$format = str_replace('%link', $link, $format);
echo $format;
}
add_filter('next_post_link', 'shrink_next_post_link',10,2);
add_filter('previous_post_link', 'shrink_previous_post_link',10,2);
我需要做的一切。谢谢!
答案 1 :(得分:0)
next_post_link()
和previous_post_link()
功能都带有自定义选项。 http://codex.wordpress.org/Function_Reference/next_post_link。在阅读并了解函数的可接受参数之后,请测试是否可以将php函数传递给该选项,如substr()。
<?php next_post_link('%link',substr('%title',20),FALSE);?>
'%link'和'%title'是帖子链接和标题的简码。
让我们知道它是否成功。