【问题标题】:How can I display only resources from a specific year with getResources (Modx)?如何使用 getResources (Modx) 仅显示特定年份的资源?
【发布时间】:2016-04-18 23:53:24
【问题描述】:

我只想显示 2015 年发布的资源。

我试过了:

&where=`{"publishedon:":2015}`

但这不起作用。有人可以帮帮我吗?

【问题讨论】:

    标签: modx modx-getresources


    【解决方案1】:

    publishedon 在数据库中保存为 unix 时间。因此,您必须将日期(2015 年)转换为 unix 时间并检查日期范围。

    使用以下代码创建一个名为 inyear 的 sn-p:

    <?php
    $year = $modx->getOption('year', $scriptProperties, '');
    $where = $modx->getOption('where', $scriptProperties, false);
    $where = is_array($where) ? $where : json_decode($where, true);
    $where = ($where) ? $where : array();
    
    if ($year) {
        $where[] = array(
            'publishedon:>=' => strtotime('1-1-'.$year),
            'publishedon:<' => strtotime('1-1-'.($year+1)),
        );
    }
    return json_encode($where);
    

    在此之后,您可以调用 getRessources

    &where=`[[inyear? &year=`2015`]]`
    

    您甚至可以使用 inyear sn-p 的 &where 属性添加额外的 where 子句。

    &where=`[[inyear? &year=`2015` &where=`whatever_else_where`]]`
    

    【讨论】:

    • 谢谢。不幸的是,这不起作用。使用您的代码,我的所有岁月仍然显示
    • 应该可以了,可以直接调用sn-p inyear [[inyear? &amp;year=`2015`] 并查找输出。
    • 我就像你说的那样调用了 sn-p。没有输出
    • 抱歉,我忘记在 getOption 方法调用中添加 $scriptProperties。
    猜你喜欢
    • 2014-02-12
    • 2016-06-13
    • 2013-11-11
    • 1970-01-01
    • 2019-07-30
    • 2014-05-18
    • 1970-01-01
    • 1970-01-01
    • 2014-10-17
    相关资源
    最近更新 更多