Short answer: get_previous_posts_page_link
是您想要的功能:
<?php if($url = get_previous_posts_page_link()): ?>
do stuff with $url
<?php endif; ?>
Longer answer: 遵循代码。
previous_posts_link
呼叫get_previous_posts_link
.
<?php
/**
* Display the previous posts page link.
*
* @since 0.71
* @uses get_previous_posts_link()
*
* @param string $label Optional. Previous page link text.
*/
function previous_posts_link( $label = null ) {
echo get_previous_posts_link( $label );
}
get_previous_posts_link
呼叫
previous_posts
获取链接URI本身。
<?php
/**
* Return the previous posts page link.
*
* @since 2.7.0
*
* @param string $label Optional. Previous page link text.
* @return string|null
*/
function get_previous_posts_link( $label = null ) {
global $paged;
if ( null === $label )
$label = __( \'« Previous Page\' );
if ( !is_single() && $paged > 1 ) {
$attr = apply_filters( \'previous_posts_link_attributes\', \'\' );
return \'<a href="\' . previous_posts( false ) . "\\" $attr>". preg_replace( \'/&([^#])(?![a-z]{1,8};)/i\', \'&$1\', $label ) .\'</a>\';
}
}
previous_posts
只是您要查找的函数的一个小包装器:
get_previous_posts_page_link
<?php
/**
* Display or return the previous posts page link.
*
* @since 0.71
*
* @param boolean $echo Optional. Echo or return;
*/
function previous_posts( $echo = true ) {
$output = esc_url( get_previous_posts_page_link() );
if ( $echo )
echo $output;
else
return $output;
}
我们想要的功能:
<?php
/**
* Retrieve previous posts page link.
*
* Will only return string, if not on a single page or post.
*
* Backported to 2.0.10 from 2.1.3.
*
* @since 2.0.10
*
* @return string|null
*/
function get_previous_posts_page_link() {
global $paged;
if ( !is_single() ) {
$nextpage = intval($paged) - 1;
if ( $nextpage < 1 )
$nextpage = 1;
return get_pagenum_link($nextpage);
}
}
我包括了所有这些,以说明如何通过查看核心来找到答案。工具,如
ack 可以帮助您开始:
shell$ cd /path/to/your/wordpress/install
shell$ ack "function previous_posts_link"
一般来说,WordPress非常擅长确保函数只做一件事。从更广泛的功能(ala)进行跟踪
previous_posts_link
和其他模板标签)返回到更基本的函数通常是学习一些很酷的东西并找到答案的好方法。