get_month_link( bool|int $year, bool|int $month )

Retrieves the permalink for the month archives with year.


Description Description


Parameters Parameters

$year

(bool|int) (Required) False for current year. Integer of year.

$month

(bool|int) (Required) False for current month. Integer of month.


Top ↑

Return Return

(string) The permalink for the specified month and year archive.


Top ↑

Source Source

File: wp-includes/link-template.php

function get_month_link( $year, $month ) {
	global $wp_rewrite;
	if ( ! $year ) {
		$year = current_time( 'Y' );
	}
	if ( ! $month ) {
		$month = current_time( 'm' );
	}
	$monthlink = $wp_rewrite->get_month_permastruct();
	if ( ! empty( $monthlink ) ) {
		$monthlink = str_replace( '%year%', $year, $monthlink );
		$monthlink = str_replace( '%monthnum%', zeroise( intval( $month ), 2 ), $monthlink );
		$monthlink = home_url( user_trailingslashit( $monthlink, 'month' ) );
	} else {
		$monthlink = home_url( '?m=' . $year . zeroise( $month, 2 ) );
	}

	/**
	 * Filters the month archive permalink.
	 *
	 * @since 1.5.0
	 *
	 * @param string $monthlink Permalink for the month archive.
	 * @param int    $year      Year for the archive.
	 * @param int    $month     The month for the archive.
	 */
	return apply_filters( 'month_link', $monthlink, $year, $month );
}

Top ↑

Changelog Changelog

Changelog
Version Description
1.0.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 3 content
    Contributed by Codex

    Use With PHP Variables

    PHP code block for use within The Loop: Assigns year and month of a post to the variables $arc_year and $arc_month. These are used with the get_month_link() tag, which returns the URL as a link to the monthly archive for that 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'); ?>
    <?php $archive_month = get_the_time('m'); ?>
    
    <a href="<?php echo get_month_link( $archive_year, $archive_month ); ?>">
    Archive for <?php the_time('F Y'); ?>
    </a>
    

You must log in before being able to contribute a note or feedback.