WordPress 主题制作技巧之一 [ 为 previous_post_link 等链接添加 title 属性 ]

使用 WordPress 自己搭建博客的网友可能越来越多,我用它也有一段时间了,尽管运行效率并不让人满意,但其丰富的插件让人着迷。WordPress 虽然有着众多的免费主题,但我想还是有很多朋友还是倾向于自定义个性鲜明的主题。我打算做一个极其简易的主题,让我的 WordPress 程序像个日记本一样一页一页地翻阅。制作主题和调整程序的过程中遇到一些小问题,本着学习的态度现在把它们记下来,也希望能给遇到这些问题的人提供一点我的意见。

今天我要说的是:为 previous_post_link 等链接添加 title 属性

previous_post_link 和 lt_next_post_link 是经常用于单文章页面 single.php 中的用来链接上一篇和下一篇文章的函数,用过这两个函数的人会发现,函数执行结果在前台输出之后,当访客的鼠标滑过时是没有任何提示的,这在有时会造成一定的不便,为了解决这个问题,我决定给 previous_post_link 和 lt_next_post_link 加上 title 属性。

先找到 previous_post_link、lt_next_post_link 以及与其相关的 adjacent_post_link 函数的代码,其代码位于根目录下的 wp-includes/link-template.php 文件中(大约是在1300多行的样子)。当然可以从这个文件里面直接改 previous_post_link、next_post_link、adjacent_post_link 这三个函数,但是考虑到 link-template.php 是 WordPress 的核心文件,以后不知道哪次升级时就会被替换,所以我在模板文件夹中的 functions.php 中新建了三个函数 (lt_previous_post_link、lt_next_post_link、lt_adjacent_post_link) ,新建的这三个函数在原来函数的基础上增加了 title 属性。这样我模板文件中调用新建的 lt_previous_post_link 和 lt_next_post_link 来链接上一篇和下一篇文章。

代码如下:

1. previous_post_link、lt_next_post_link、adjacent_post_link 函数的代码
/**
 * Display previous post link that is adjacent to the current post.
 *
 * @since 1.5.0
 *
 * @param string $format Optional. Link anchor format.
 * @param string $link Optional. Link permalink format.
 * @param bool $in_same_cat Optional. Whether link should be in same category.
 * @param string $excluded_categories Optional. Excluded categories IDs.
 */
function previous_post_link($format='« %link', $link='%title', $in_same_cat = false, $excluded_categories = '') {
	adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, true);
}

/**
 * Display next post link that is adjacent to the current post.
 *
 * @since 1.5.0
 *
 * @param string $format Optional. Link anchor format.
 * @param string $link Optional. Link permalink format.
 * @param bool $in_same_cat Optional. Whether link should be in same category.
 * @param string $excluded_categories Optional. Excluded categories IDs.
 */
function next_post_link($format='%link »', $link='%title', $in_same_cat = false, $excluded_categories = '') {
	adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, false);
}

/**
 * Display adjacent post link.
 *
 * Can be either next post link or previous.
 *
 * @since 2.5.0
 *
 * @param string $format Link anchor format.
 * @param string $link Link permalink format.
 * @param bool $in_same_cat Optional. Whether link should be in same category.
 * @param string $excluded_categories Optional. Excluded categories IDs.
 * @param bool $previous Optional, default is true. Whether display link to previous post.
 */
function adjacent_post_link($format, $link, $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;

	$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);

	$adjacent = $previous ? 'previous' : 'next';
	echo apply_filters( "{$adjacent}_post_link", $format, $link );
}

2. lt_previous_post_link、lt_next_post_link、lt_adjacent_post_link 函数的代码
/**
 * Display previous post link that is adjacent to the current post.
 *
 * @since 1.5.0
 *
 * @param string $format Optional. Link anchor format.
 * @param string $link Optional. Link permalink format.
 * @param bool $in_same_cat Optional. Whether link should be in same category.
 * @param string $link_title Optional. Define the link title what is display when mouse hover the link.
 * @param string $excluded_categories Optional. Excluded categories IDs.
 */
function lt_previous_post_link($format='« %link', $link='%title', $in_same_cat = false, $link_title = '', $excluded_categories = '') {
	st_adjacent_post_link($format, $link, $in_same_cat, $link_title, $excluded_categories, true);
}

/**
 * Display next post link that is adjacent to the current post.
 *
 * @since 1.5.0
 *
 * @param string $format Optional. Link anchor format.
 * @param string $link Optional. Link permalink format.
 * @param bool $in_same_cat Optional. Whether link should be in same category.
 * @param string $link_title Optional. Define the link title what is display when mouse hover the link.
 * @param string $excluded_categories Optional. Excluded categories IDs.
 */
function lt_next_post_link($format='%link »', $link='%title', $in_same_cat = false, $link_title = '', $excluded_categories = '') {
	st_adjacent_post_link($format, $link, $in_same_cat, $link_title, $excluded_categories, false);
}

/**
 * Display adjacent post link.
 *
 * Can be either next post link or previous.
 *
 * @since 2.5.0
 *
 * @param string $format Link anchor format.
 * @param string $link Link permalink format.
 * @param bool $in_same_cat Optional. Whether link should be in same category.
 * @param string $link_title Optional. Define the link title what is display when mouse hover the link.
 * @param string $excluded_categories Optional. Excluded categories IDs.
 * @param bool $previous Optional, default is true. Whether display link to previous post.
 */
function lt_adjacent_post_link($format, $link, $in_same_cat = false, $link_title = '', $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;

	$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).'" title="'.$link_title.'" rel="'.$rel.'">';
	$link = str_replace('%title', $title, $link);
	$link = str_replace('%date', $date, $link);
	$link = $string . $link . '</a>';

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

	$adjacent = $previous ? 'previous' : 'next';
	echo apply_filters( "{$adjacent}_post_link", $format, $link );
}

效果如下:
 
1. 在模板中调用 previous_post_link、next_post_link 的效果
 

2. 在模板中调用 lt_previous_post_link、lt_next_post_link 的效果
 


OK,完成。
 
2011.03

posted @ 2011-03-11 22:29  刘笨笨  阅读(4452)  评论(2编辑  收藏  举报