query() public static method

Convenience function that takes a standard set of WP_Query arguments but mixes it with arguments that mean we're selecting the right post type
public static query ( array $args = null ) : array
$args array standard WP_Query array
return array An array of Post objects
示例#1
0
<?php

/**
 * Search results page
 *
 * Methods for TimberHelper can be found in the /lib sub-directory
 *
 * @package  WordPress
 * @subpackage  Timber
 * @since   Timber 0.1
 */
use Lumberjack\PostTypes\Post;
$templates = ['search.twig', 'posts.twig', 'generic-page.twig'];
$context = Timber::get_context();
$searchQuery = get_search_query();
$context['title'] = 'Search results for ' . $searchQuery;
$context['posts'] = Post::query(['s' => $searchQuery]);
Timber::render($templates, $context);
示例#2
0
<?php

/**
 * The template for displaying Author Archive pages
 *
 * Methods for TimberHelper can be found in the /lib sub-directory
 *
 * @package  WordPress
 * @subpackage  Timber
 * @since    Timber 0.1
 */
global $wp_query;
use Lumberjack\PostTypes\Post;
$data = Timber::get_context();
if (isset($wp_query->query_vars['author'])) {
    $author = new TimberUser($wp_query->query_vars['author']);
    $data['author'] = $author;
    $data['title'] = 'Author Archives: ' . $author->name();
    $data['posts'] = Post::query(['author' => $author->ID]);
}
Timber::render(['author.twig', 'posts.twig', 'generic-page.twig'], $data);
示例#3
0
 *
 * Learn more: http://codex.wordpress.org/Template_Hierarchy
 *
 * Methods for TimberHelper can be found in the /lib sub-directory
 *
 * @package  WordPress
 * @subpackage  Timber
 * @since   Timber 0.2
 */
use Lumberjack\PostTypes\Post;
$templates = ['posts.twig', 'generic-page.twig'];
$data = Timber::get_context();
$data['title'] = 'Archive';
if (is_day()) {
    $data['title'] = 'Archive: ' . get_the_date('D M Y');
} elseif (is_month()) {
    $data['title'] = 'Archive: ' . get_the_date('M Y');
} elseif (is_year()) {
    $data['title'] = 'Archive: ' . get_the_date('Y');
} elseif (is_tag()) {
    $data['title'] = single_tag_title('', false);
} elseif (is_category()) {
    $data['title'] = single_cat_title('', false);
    array_unshift($templates, 'archive-' . get_query_var('cat') . '.twig');
} elseif (is_post_type_archive()) {
    $data['title'] = post_type_archive_title('', false);
    array_unshift($templates, 'archive-' . get_post_type() . '.twig');
}
// TODO: Currently only works for posts, fix for custom post types
$data['posts'] = Post::query();
Timber::render($templates, $data);