我需要定製WordPress上的previous_post_link()
和next_post_link()
。如何自定義WordPress的內部功能,如adjacent_post_link()
舉例來說,我想縮小永久鏈接,比如「需要學習的前5種編程語言」到「前5個編程...」。
負責創建位於鏈路isadjacent_post_link()
功能在wp-includes/link-template.php
我需要定製WordPress上的previous_post_link()
和next_post_link()
。如何自定義WordPress的內部功能,如adjacent_post_link()
舉例來說,我想縮小永久鏈接,比如「需要學習的前5種編程語言」到「前5個編程...」。
負責創建位於鏈路isadjacent_post_link()
功能在wp-includes/link-template.php
的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'是後鏈接和標題的簡碼。
讓我們知道它是否工作。
嘿kevtrout! 感謝回放。你的想法不起作用,但有助於弄清楚我自己的想法。 首先,substr是不可能的,因爲wordpress代碼不執行函數的參數。 但仔細看代碼,我看到了一個鉤子,我使用它。所以,我會回答這個問題,所以每個人都可以從中受益。 – 2010-08-25 14:52:13
要爲帖子創建自定義相鄰鏈接,我可以使用過濾器掛鉤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);
這一切我需要做的。謝謝!
嗯,我需要限制下一個帖子和prev帖子鏈接上的帖子,以避免錯誤 - 請參閱示例:http://www.noamdesign.com/blog/time-doesnt-matter-part-2/ – 2010-08-22 18:28:58