我一直在用Timber来获取帖子列表。它运行得很好,但是我需要像这样“按日期排序”:
=1,2=邮政5邮政4=1月,1=邮政3邮政2 Pos 1
WordPress函数the_date()将执行此操作,但我无法获得与Timber相同的结果。the_date返回空
Index.php
$context = Timber::context();
// $context['posts'] = new Timber\PostQuery();
$context['posts'] = Timber::get_posts(); // both doesn't work
$templates = array( 'archive.twig', 'index.twig' );
Timber::render( $templates, $context );Archive.twig
<div class="s-archive__posts-list">
{% for post in posts %}
{{ the_date() }} // function registered via $twig->addFunction( new Timber\Twig_Function( 'the_date', 'the_date' ) );
{{ fn('the_date') }} - doesn't work too. I was guessing, maybe, fourth param of this function the case (echo) but it's not {{ fn('the_date', 'M, Y', '', '', false ) }}.
{% include 'content/content-archive.twig' %}
{% endfor %}
</div><!-- /s-archive__posts-list -->我要做什么才能使它如我所期望的那样运作呢?
它使用的更新
$context‘’posts‘= Timber::query_posts();
发布于 2020-09-19 10:50:51
问题是,对Wordpress函数the_date()的调用将返回null。
这是因为如果the_date函数没有其他参数,并且不返回值,它只会回显日期。对the_date的完整调用具有以下形式
the_date(字符串$format = '',字符串$before = '',字符串$after = '',bool $echo = true )
echo的默认值为true --如果希望它返回值,则需要将此参数设置为false。请参阅https://developer.wordpress.org/reference/functions/the_date/
以获得完整的描述。
Wordpress还提供了一个get_the_date函数,它不回显,但总是返回日期--参见https://developer.wordpress.org/reference/functions/get_the_date/
我不熟悉木材,所以不知道什么是最好的,确切地说,你正在努力实现。
发布于 2020-12-30 16:01:34
如果使用像the_date()这样的函数,则该函数将依赖于设置为全局的$post变量。然而,在Timber中,我们不依赖$post全局,因为通常情况下,它更难控制。
相反,我们使用post本身的属性和方法。对于帖子的日期,您可以使用{{ post.date }}。
{% for post in posts %}
{{ post.date }}
{% include 'content/content-archive.twig' %}
{% endfor %}这样,您也不必向$twig->addFunction()注册函数。
https://stackoverflow.com/questions/63967664
复制相似问题