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; }
/** * Logic for rendering this shortcode * * Note that we return the output of this shortcode. This is a requirement * by WordPress based on how do_shortcode works. * @return string * Compiled shortcode */ public function render($atts = array(), $content = null) { $template = $this->shortcode . '.twig'; if ($this->template !== null) { $template = $this->template; } $template = \apply_filters('shortcode_template', $template, $this->shortcode); if (class_exists('\\Timber\\Timber')) { return Timber::compile($template, $this->data); } else { include_once $template; } }
} } // theme sidebars if ($container->hasParameter('wordpress.sidebars')) { foreach ($container->getParameter('wordpress.sidebars') as $sidebar) { register_sidebar($sidebar); } } // menus from config if ($container->hasParameter('wordpress.menus')) { register_nav_menus($container->getParameter('wordpress.menus')); } }); add_action('wp_head', function () { if (function_exists('acf_add_options_page') && ($googleID = get_field('google_analytics_id', 'option'))) { Timber::render('admin/google.html.twig', ['id' => $googleID]); } }); /*********************************************************************************************************************** * Filters **********************************************************************************************************************/ // save acf as json add_filter('acf/settings/save_json', function ($path) use($container) { return $container->getParameterBag()->resolveValue($container->getParameter('wordpress.acf_path')); }); // show acf menus add_filter('acf/settings/show_admin', function ($show) use($container) { return $container->getParameter('wordpress.acf_menu'); }); // global timber context add_filter('timber/context', function ($data) {
/** * Get context. * @api * @return array */ public static function get_context() { if (empty(self::$context_cache)) { self::$context_cache['http_host'] = 'http://' . URLHelper::get_host(); self::$context_cache['wp_title'] = Helper::get_wp_title(); self::$context_cache['wp_head'] = Helper::function_wrapper('wp_head'); self::$context_cache['wp_footer'] = Helper::function_wrapper('wp_footer'); self::$context_cache['body_class'] = implode(' ', get_body_class()); self::$context_cache['site'] = new Site(); self::$context_cache['request'] = new Request(); $user = new User(); self::$context_cache['user'] = $user->ID ? $user : false; self::$context_cache['theme'] = self::$context_cache['site']->theme; self::$context_cache['posts'] = Timber::query_posts(); self::$context_cache = apply_filters('timber_context', self::$context_cache); self::$context_cache = apply_filters('timber/context', self::$context_cache); } return self::$context_cache; }
<?php namespace Trendwerk\Sphynx; use Timber\Timber; $timber = new Timber(); Timber::$dirname = array('templates/base', 'templates'); $theme = new Theme(); $theme->init();
/** * 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); }
<?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);
/** * @return \Twig_Environment */ public function get_twig() { $loader = $this->get_loader(); $params = array('debug' => WP_DEBUG, 'autoescape' => false); if (isset(Timber::$autoescape)) { $params['autoescape'] = Timber::$autoescape; } if (Timber::$cache === true) { Timber::$twig_cache = true; } if (Timber::$twig_cache) { $twig_cache_loc = apply_filters('timber/cache/location', TIMBER_LOC . '/cache/twig'); if (!file_exists($twig_cache_loc)) { mkdir($twig_cache_loc, 0777, true); } $params['cache'] = $twig_cache_loc; } $twig = new \Twig_Environment($loader, $params); if (WP_DEBUG) { $twig->addExtension(new \Twig_Extension_Debug()); } $twig->addExtension($this->_get_cache_extension()); $twig = apply_filters('twig_apply_filters', $twig); $twig = apply_filters('timber/twig/filters', $twig); $twig = apply_filters('timber/twig/escapers', $twig); $twig = apply_filters('timber/loader/twig', $twig); return $twig; }
<?php require_once __DIR__ . '/app/bootstrap.php'; use Timber\Timber; /** @var $timber Timber */ $timber = $container->get('timber'); $context = Timber::get_context(); $post = Timber::query_post(); $context['post'] = $post; if (post_password_required($post->ID)) { Timber::render('single-password.html.twig', $context); } else { Timber::render(['single-' . $post->ID . '.html.twig', 'single-' . $post->post_type . '.html.twig', 'single.html.twig'], $context); }
<?php require_once __DIR__ . '/app/bootstrap.php'; use Timber\Timber; use Timber\Post; /** @var $timber Timber */ $timber = $container->get('timber'); $context = Timber::get_context(); $post = new Post(); $context['post'] = $post; Timber::render(['page-' . $post->post_name . '.twig', 'page.html.twig'], $context);
<?php require_once __DIR__ . '/app/bootstrap.php'; use Timber\Timber; /** @var $timber Timber */ $timber = $container->get('timber'); $templates = ['search.html.twig', 'archive.html.twig', 'index.html.twig']; $context = Timber::get_context(); $context['title'] = 'Search results for ' . get_search_query(); $context['posts'] = Timber::get_posts(); Timber::render($templates, $context);
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]; }
/** * 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)); }
<?php use Timber\Timber; $timber = new Timber(); Timber::$dirname = 'views';