get_posts() публичный статический Метод

Get posts.
public static get_posts ( mixed $query = false, string | array $PostClass = 'Timber\Post', $return_collection = false ) : array | boolean | null
$query mixed
$PostClass string | array
Результат array | boolean | null
 protected function get_posts()
 {
     // Create start date, in seconds
     $time = time('now') - $this->time;
     $date = date('Y-m-d', $time);
     $query_args = array('post_type' => $this->post_types, 'posts_per_page' => $this->limit, 'date_query' => array(array('after' => $date)));
     $posts = Timber::get_posts($query_args, 'bermanco\\ExtendedTimberClasses\\Post');
     return $posts;
 }
Пример #2
0
 /**
  * Get Posts that have been "tagged" with the particular term
  * @internal
  * @param int $numberposts
  * @param string $post_type
  * @param string $PostClass
  * @return array|bool|null
  */
 public function get_posts($numberposts = 10, $post_type = 'any', $PostClass = '')
 {
     if (!strlen($PostClass)) {
         $PostClass = $this->PostClass;
     }
     $default_tax_query = array(array('field' => 'id', 'terms' => $this->ID, 'taxonomy' => $this->taxonomy));
     if (is_string($numberposts) && strstr($numberposts, '=')) {
         $args = $numberposts;
         $new_args = array();
         parse_str($args, $new_args);
         $args = $new_args;
         $args['tax_query'] = $default_tax_query;
         if (!isset($args['post_type'])) {
             $args['post_type'] = 'any';
         }
         if (class_exists($post_type)) {
             $PostClass = $post_type;
         }
     } else {
         if (is_array($numberposts)) {
             //they sent us an array already baked
             $args = $numberposts;
             if (!isset($args['tax_query'])) {
                 $args['tax_query'] = $default_tax_query;
             }
             if (class_exists($post_type)) {
                 $PostClass = $post_type;
             }
             if (!isset($args['post_type'])) {
                 $args['post_type'] = 'any';
             }
         } else {
             $args = array('numberposts' => $numberposts, 'tax_query' => $default_tax_query, 'post_type' => $post_type);
         }
     }
     return Timber::get_posts($args, $PostClass);
 }
Пример #3
0
<?php

require_once __DIR__ . '/app/bootstrap.php';
use Timber\Timber;
use Timber\User;
/** @var $timber Timber */
$timber = $container->get('timber');
$context = Timber::get_context();
$context['posts'] = Timber::get_posts();
if (isset($wp_query->query_vars['author'])) {
    $author = new User($wp_query->query_vars['author']);
    $context['author'] = $author;
    $context['title'] = 'Author Archives: ' . $author->name();
}
$timber::render(['author.html.twig', 'archive.html.twig'], $context);
Пример #4
0
function base_randomItem($type = 'page')
{
    $args = array('post_type' => $type, 'numberposts' => 1, 'orderby' => 'rand');
    $rand_posts = \Timber\Timber::get_posts($args);
    return $rand_posts[0];
}
Пример #5
0
 /**
  * Wraps Timber::get_posts($query) and intitialzes the each post as the correct
  * CustomPostType class.
  * @param mixed $query
  * @return array
  */
 public static function getPosts($query = false)
 {
     $namespace = $namespace = static::getSiteNamespace() . '\\Models\\';
     return array_map(function ($post) use($namespace) {
         $className = $namespace . $post->post_type;
         if (class_exists($className)) {
             return new $className($post);
         }
         return $post;
     }, Timber::get_posts($query));
 }