【问题标题】:How to use modx getResources to return the parent resource one time only?如何使用 modx getResources 只返回一次父资源?
【发布时间】:2014-05-18 02:59:17
【问题描述】:

如何使用 getResources 从资源树中的多个节点获取父资源〜仅一次〜即我有一个 getResources 调用:

[[!getResources?  
    &parents=`738,746,1222, 748, 1216, 805, 806, 807, 3401`
    &tpl=`SecondaryUpdatesHomePageTpl`  
    &limit=`3`  
    &includeTVs=`1`
    &processTVs=`1`  
    &hideContainers=`1`  
    &includeContent=`0`   
    &depth=`1`  
    &sortby=`{"createdon":"desc"}`
]]

&parents id 是要搜索的树,每个父级中可能有几个新资源。我需要获取资源来返回 &parents 项目的父资源'但只有一次'

例如,如果父母 738、748、807 和 3401 下各有 4 或 5 个新资源,我不希望 738 被退回 3 次,我需要 ~738、748 和 807 的父母退回。

关于如何做到这一点的任何想法? [modx 革命 2.2.12]

【问题讨论】:

  • 没有资源树的截图很难理解。

标签: modx modx-revolution getresource


【解决方案1】:

看了一段时间后,我想您基本上只需执行另一个 getResources 调用,并将 &resources 参数设置为第一个 getResources 调用的修改输出。请注意,我从嵌套的 getResources 调用中删除了 includeTV。

更改 getResources 中的 TPL 以输出父项,后跟逗号,([[+parent]],) 基本上生成一个逗号分隔的 ID 列表。将 getResources 调用的输出提供给另一个具有正确 TPL (SecondaryUpdatesHomePageTpl) 的调用

[[!getResources? 
  &resources=`[[!getResources?  
    &parents=`738,746,1222, 748, 1216, 805, 806, 807, 3401`
    &tpl=`CSVListOfParentsTPL`  
    &limit=`3`  
    &hideContainers=`1`  
    &depth=`1`  
    &sortby=`{"createdon":"desc"}`
  ]]` 
  &tpl=`SecondaryUpdatesHomePageTpl`
  &includeTVs=`1`
  &processTVs=`1`  
]]

这样的嵌套显然不是最有效的解决方案,替代方法是编写您自己的自定义 sn-p,使用与您提供 getResources 的参数或多或少相同的参数进行 $modx->查询,但获取父母直接而不是第二次去得到它们。

【讨论】:

  • 看起来仍然会多次返回同一个父级。我将不得不在 getresources 上使用 runSnippet,然后自己过滤结果。不过还是谢谢。
【解决方案2】:

尝试使用新的sn-p:

[[unikids? &input=`1,5,6` $depth=`5`]]

和片段代码:

<?php

$array_big = array();
$inputids = explode(",",$input);
foreach($inputids as $inputid) {

 $array_ids = $modx->getChildIds($inputid,$depth,array('context' => 'web'));

        /// add to master array
        $array_big = array_merge ($array_big, $array_ids);
   } 

   $output = implode(",",array_unique($array_big));

 return $output;

?>

这个片段输出唯一的 id 到你的 getResources 参数:

[[!getResources? 
  &resources=`[[unikids? &input=`1,5,6` $depth=`5`]]` 
  &tpl=`SecondaryUpdatesHomePageTpl`
  &includeTVs=`1`
  &processTVs=`1`  
]]

【讨论】:

    猜你喜欢
    • 2013-11-11
    • 2016-01-16
    • 2016-06-13
    • 2016-04-18
    • 2014-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多