get_year_link()获取年份存档页面链接

目录

描述

Returns the yearly archive URL to a specific year for use in PHP. It does NOT display the URL. If year is set to '', the tag returns the URL for the current year's archive.

get_year_link() 描述

用法

 <?php get_year_link$year ); ?> 

get_year_link() 用法

参数

year

(integer) (可选) The year for the archive. Use '' to assign current year.

默认值: None

get_year_link() 参数

示例

Year as Link

Returns the URL for the current year's archive, displaying it as a link in the anchor tag by using the PHP echo command.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

<a href="<?php echo get_year_link(''); ?>">Posts from this year</a>

Year as a variable

Returns URL for the archive year 2003, assigning it to the variable $year03. The variable can then be used elsewhere in a page.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

<?php $year03 = get_year_link(2003); ?>

Using With PHP Variables

PHP code block for use within The Loop: Assigns year to the variable $arc_year. This is used with the get_year_link() tag, which returns the URL as a link to the yearly archive for a post, displaying it within an anchor tag with the PHP echo command. See Formatting Date and Time for info on format strings used in get_the_time() tag.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

<?php $archive_year = get_the_time('Y'); ?>

<a href="<?php echo get_year_link( $archive_year ); ?>"><?php the_time('Y'); ?> archive</a>

get_year_link() 示例

源文件

get_year_link() 函数的代码位于 wp-includes/link-template.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Retrieve the permalink for the year archives.
 *
 * @since 1.5.0
 *
 * @global WP_Rewrite $wp_rewrite
 *
 * @param int|bool $year False for current year or year for permalink.
 * @return string The permalink for the specified year archive.
 */
function get_year_link($year) {
	global $wp_rewrite;
	if ( !$year )
		$year = gmdate('Y', current_time('timestamp'));
	$yearlink = $wp_rewrite->get_year_permastruct();
	if ( !empty($yearlink) ) {
		$yearlink = str_replace('%year%', $year, $yearlink);
		$yearlink = home_url( user_trailingslashit( $yearlink, 'year' ) );
	} else {
		$yearlink = home_url( '?m=' . $year );
	}

	/**
	 * Filter the year archive permalink.
	 *
	 * @since 1.5.0
	 *
	 * @param string $yearlink Permalink for the year archive.
	 * @param int    $year     Year for the archive.
	 */
	return apply_filters( 'year_link', $yearlink, $year );
}

get_year_link() 源文件

相关