Skip to main content
Timber

You are reading the documentation for Timber v2.x. Switch to the documentation for Timber v1.x.

Timber\​Archives

The Timber\Archives class is used to generate a menu based on the date archives of your posts.

The Nieman Foundation News site has an example of how the output can be used in a real site (screenshot).

PHP

$context['archives'] = new Timber\Archives( $args );

Twig

<ul>
{% for item in archives.items %}
<li><a href="{{item.link}}">{{item.name}}</a></li>
{% for child in item.children %}
<li class="child"><a href="{{child.link}}">{{child.name}}</a></li>
{% endfor %}
{% endfor %}
</ul>

HTML

<ul>
<li>2015</li>
<li class="child">May</li>
<li class="child">April</li>
<li class="child">March</li>
<li class="child">February</li>
<li class="child">January</li>
<li>2014</li>
<li class="child">December</li>
<li class="child">November</li>
<li class="child">October</li>
</ul>

Overview #

This class extends Timber\Core

Properties #

NameTypeDescription
$basestring
$itemsarrayThe items of the archives to iterate through and markup for your page.

Methods #

NameReturn TypeSummary/Returns
__call()mixedMagic method dispatcher for meta fields, for convenience in Twig views.

Returns: The value of the meta field named $field if truthy, false otherwise.
__construct()Build an Archives menu
get_items()array or stringGets archive items.
items()array or string

Class Methods #

__call() #

Magic method dispatcher for meta fields, for convenience in Twig views.

Called when explicitly invoking non-existent methods on a Core object. This method is not meant to be called directly.

link https://secure.php.net/manual/en/language.oop5.overloading.php#object.call

__call( string $field, array $arguments )

Returns: mixed The value of the meta field named $field if truthy, false otherwise.

This method is inherited from \Timber\Core.

NameTypeDescription
$fieldstringThe name of the method being called.
$argumentsarrayEnumerated array containing the parameters passed to the function. Not used.

PHP

$post = Timber\Post::get( get_the_ID() );

update_post_meta( $post->id, 'favorite_zep4_track', 'Black Dog' );

Timber::render( 'rock-n-roll.twig', array( 'post' => $post ) );

Twig

{# Since this method does not exist explicitly on the Post class,
it will dynamically dispatch the magic __call() method with an argument
of "favorite_zep4_track" #}

<span>Favorite <i>Zeppelin IV</i> Track: {{ post.favorite_zep4_track() }}</span>

__construct() #

Build an Archives menu

__construct( array $args = null, string $base = '' )

NameTypeDescription
$argsarrayOptional. Array of arguments.

  • $show_year
    bool false
  • $
    string
  • $type
    string 'monthly-nested'
  • $limit
    int -1
  • $show_post_count
    bool false
  • $order
    string 'DESC'
  • $post_type
    string 'post'
  • $show_year
    bool false
  • $nested
    bool false
$basestringAny additional paths that need to be prepended to the URLs that are generated, for example: "tags". Default ''.

get_items() #

Gets archive items.

DEPRECATED since 2.0.0, use {{ archives.items }} instead.

see Timber\Archives::items()

get_items( mixed $args = null )

Returns: array|string


items() #

items( array|string $args = null )

Returns: array|string

NameTypeDescription
$argsarray or stringOptional. Array of arguments.