首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WordPress the_date()不适用于Timber

WordPress the_date()不适用于Timber
EN

Stack Overflow用户
提问于 2020-09-19 10:21:08
回答 2查看 347关注 0票数 1

我一直在用Timber来获取帖子列表。它运行得很好,但是我需要像这样“按日期排序”:

=1,2=邮政5邮政4=1月,1=邮政3邮政2 Pos 1

WordPress函数the_date()将执行此操作,但我无法获得与Timber相同的结果。the_date返回空

Index.php

代码语言:javascript
复制
$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

代码语言:javascript
复制
<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();

EN

回答 2

Stack Overflow用户

发布于 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/

我不熟悉木材,所以不知道什么是最好的,确切地说,你正在努力实现。

票数 0
EN

Stack Overflow用户

发布于 2020-12-30 16:01:34

如果使用像the_date()这样的函数,则该函数将依赖于设置为全局的$post变量。然而,在Timber中,我们不依赖$post全局,因为通常情况下,它更难控制。

相反,我们使用post本身的属性和方法。对于帖子的日期,您可以使用{{ post.date }}

代码语言:javascript
复制
{% for post in posts %}
    {{ post.date }}

    {% include 'content/content-archive.twig' %}
{% endfor %}

这样,您也不必向$twig->addFunction()注册函数。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63967664

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档