get_year_link() - WordPress 函数参考中文文档

返回介绍

get_year_link()

发布于 2017-09-11 字数3643 浏览 1034 评论 0

get_year_link( int|bool $year )

Retrieves the permalink for the year archives.


描述


参数

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

更新日志

Version 描述
1.5.0 Introduced.

相关函数

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>
    

上一篇:get_wp_title_rss()

下一篇:global_terms()

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!