如何自定义wordpress内部函数,例如neighbor_post_link() [英] how to customize wordpress internal functions, like adjacent_post_link()
问题描述
我需要在WordPress上自定义 previous_post_link()
和 next_post_link()
.
每个例子,我想将诸如您需要学习的5种编程语言"之类的永久链接缩小为"5种编程...".
负责创建链接的函数 isadjacent_post_link()
位于 wp-includes/link-template.php
要为帖子创建自定义的相邻链接,我可以使用过滤器钩next_post_link和previos_post_link;
在functions.php中:
functionrink_previous_post_link($ format,$ link){$ in_same_cat =假;$ excluded_categories ='';$ previous = true;$ link ='%title';$ format ='& laquo;%关联';如果($ previous&& is_attachment())$ post =&get_post($ GLOBALS ['post']-> post_parent);别的$ post = get_adjacent_post($ in_same_cat,$ excluded_categories,$ previous);如果(!$ post)返回;$ title = $ post-> post_title;如果(空($ post-> post_title))$ title = $上一个?__('上一个帖子'):__('下一个帖子');$ rel = $上一个?'prev':'next';//保存原始标题$ original_title = $ title;//创建短标题(如果需要)如果(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);回声$格式;}函数rinke_next_post_link($ format,$ link){$ in_same_cat =假;$ excluded_categories ='';$ previous = false;$ link ='%title';$ format ='%link& raquo;';如果($ previous&& is_attachment())$ post =&get_post($ GLOBALS ['post']-> post_parent);别的$ post = get_adjacent_post($ in_same_cat,$ excluded_categories,$ previous);如果(!$ post)返回;$ title = $ post-> post_title;如果(空($ post-> post_title))$ title = $上一个?__('上一个帖子'):__('下一个帖子');$ rel = $上一个?'prev':'next';//保存原始标题$ original_title = $ title;//创建短标题(如果需要)如果(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);回声$格式;}add_filter('next_post_link','shrink_next_post_link',10,2);add_filter('previous_post_link','shrink_previous_post_link',10,2);
这就是我要做的.谢谢!
I need to customize the previous_post_link()
and next_post_link()
on WordPress.
Per example, I want to shrink permalinks like "Top 5 programming languages that you need to learn" to "Top 5 programing...".
The function responsible for creating the link isadjacent_post_link()
located at wp-includes/link-template.php
To create a custom adjacent link for posts I can make use of the filter hook next_post_link and previos_post_link;
In the 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);
That all I needed to do. Thanks!
这篇关于如何自定义wordpress内部函数,例如neighbor_post_link()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!