返回介绍

get_year_link()

发布于 2017-09-11 00:46:40 字数 3641 浏览 1386 评论 0 收藏 0

get_year_link( int|bool $year )

Retrieves the permalink for the year archives.


description


参数

$year

(int|bool) (Required) False for current year or year for permalink.


返回值

(string) The permalink for the specified year archive.


源代码

File: wp-includes/link-template.php

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

	/**
	 * Filters 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 );
}

更新日志

Versiondescription
1.5.0Introduced.

相关函数

Uses

  • wp-includes/functions.php: current_time()
  • wp-includes/link-template.php: home_url()
  • wp-includes/link-template.php: year_link
  • wp-includes/link-template.php: user_trailingslashit()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/class-wp-rewrite.php: WP_Rewrite::get_year_permastruct()
  • Show 1 more use Hide more uses

Used By

  • wp-includes/general-template.php: wp_get_archives()
  • wp-includes/canonical.php: redirect_canonical()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

    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.

    
    <a href="<?php echo get_year_link(''); ?>">Posts from this year</a>
    
  2. 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.

    
    <?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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文