wp_get_archives()显示一个基于日期的存档列表

目录

描述

This function displays a date-based archives list. This tag can be used anywhere within a template.

wp_get_archives() 描述

用法

 <?php wp_get_archives$args ); ?> 

Default Usage

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php $args = array(	'type'            => 'monthly',	'limit'           => '',	'format'          => 'html', 	'before'          => '',	'after'           => '',	'show_post_count' => false,	'echo'            => 1,	'order'           => 'DESC');wp_get_archives( $args ); ?>

By default, the usage shows:

  • Monthly archives links displayed
  • Displays all archives (not limited in number)
  • Displays archives in an <li> HTML list in descending order
  • Nothing displayed before or after each link
  • Does not display the count of the number of posts

wp_get_archives() 用法

参数

type 

(string) The type of archive list to display. Defaults to WordPress settings. Valid values:

  • yearly
  • monthly - Default
  • daily
  • weekly
  • postbypost (posts ordered by post date)
  • alpha (same as postbypost but posts are ordered by post title)

limit 

(integer) Number of archives to get. Default is no limit.

format 

(string) Format for the archive list. Valid values:

  • html - In HTML list (<li>) tags and before and after strings. This is the default.
  • option - In select (<select>) or dropdown option (<option>) tags.
  • link - Within link (<link>) tags.
  • custom - Custom list using the before and after strings.

before 

(string) Text to place before the link when using the html or custom for format option. There is no default.

after 

(string) Text to place after the link when using the html or custom for format option. There is no default.

show_post_count 

(boolean) Display number of posts in an archive or do not. For use with all type except 'postbypost'.

  • 1 (True)
  • 0 (False) - Default

echo 

(boolean) Display the output or return it.

  • 1 (True) - Default
  • 0 (False)

order 

(string) How to order the link list (since Version 3.5)

  • ASC - Ascending order (A-Z).
  • DESC - Descending order (Z-A) - Default

wp_get_archives() 参数

示例

Last Twelve Months

Displays archive list by month, displaying only the last twelve months that have posts.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php wp_get_archives( array( 'type' => 'monthly', 'limit' => 12 ) ); ?>

Last Sixteen Days

Displays archive list by date, displaying only the last sixteen days.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php wp_get_archives( array( 'type' => 'daily', 'limit' => 16) ); ?>

Last Twenty Posts

Displays archive list of the last twenty most recent posts listed by post title.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php wp_get_archives( array( 'type' => 'postbypost', 'limit' => 20, 'format' => 'custom' ) ); ?>

Dropdown Box

Displays a drop-down box of monthly archives, in select tags, with the post count displayed.

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

<select name="archive-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;">
  <option value=""><?php echo esc_attr( __( 'Select Month' ) ); ?></option> 
  <?php wp_get_archives( array( 'type' => 'monthly', 'format' => 'option', 'show_post_count' => 1 ) ); ?>
</select>

To display the *ALL* posts alphabetically

Displays ALL posts alphabetically, especially if you want to have an archive that serves like a sitemap.

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

<?php wp_get_archives('type=alpha'); ?>

wp_get_archives() 示例

历史

  • 3.5.0: add 'order' parameter
  • 添加于 版本: 1.2.0

wp_get_archives() 历史

源文件

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

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Display archive links based on type and format.
 *
 * @since 1.2.0
 *
 * @see get_archives_link()
 *
 * @global wpdb      $wpdb
 * @global WP_Locale $wp_locale
 *
 * @param string|array $args {
 *     Default archive links arguments. Optional.
 *
 *     @type string     $type            Type of archive to retrieve. Accepts 'daily', 'weekly', 'monthly',
 *                                       'yearly', 'postbypost', or 'alpha'. Both 'postbypost' and 'alpha'
 *                                       display the same archive link list as well as post titles instead
 *                                       of displaying dates. The difference between the two is that 'alpha'
 *                                       will order by post title and 'postbypost' will order by post date.
 *                                       Default 'monthly'.
 *     @type string|int $limit           Number of links to limit the query to. Default empty (no limit).
 *     @type string     $format          Format each link should take using the $before and $after args.
 *                                       Accepts 'link' (`` tag), 'option' (`

wp_get_archives() 源文件

相关