如何显示通过 ExpressionEngine 中的选择元素选择的日期范围内的项目
在 ExpressionEngine 中,我有一个显示新闻项的新闻模板。我正在尝试实现一个选择元素,该元素允许用户选择特定年份,然后仅查看该年份内输入的新闻项目。
到目前为止,这是我的代码:
<select name="select_year">
<option value="">Select year</option>
<option value="2011">2011</option>
<option value="2010">2010</option>
<option value="2009">2009</option>
</select>
{exp:channel:entries
channel="news-items"
disable="categories|category_fields|trackbacks"
dynamic="off"
require_entry="yes"
sort="desc"
search:news-item-featured="featured"
limit="1"}
<div align="left">
<h2 align="center" style="margin-top: 0pt; margin-bottom: 0pt;">{title}</h2>
</div>
{if news-item-main-image}
{exp:imgsizer:size src="{news-item-main-image}" width="352"}
<p style="margin-top: 0pt;"><img align="middle" width="352" src="{sized}"></p>
{/exp:imgsizer:size}
{/if}
{news-item-intro-text}
<p><a href="{title_permalink='news-item'}">Click here to read the full story. </a></p>
{/exp:channel:entries}
<br><br>
<h3 style="margin-top: 0pt;">News History</h3>
<ul>
{exp:channel:entries
channel="news-items"
disable="categories|category_fields|trackbacks"
dynamic="off"
require_entry="yes"
limit="19"
paginate="bottom"
paginate_base="/news"}
<li><p></p><h3><a href="{title_permalink='news-item'}">{title}</a></h3>
<input type="hidden" value="" name="news_id">{entry_date format="%n/%j/%Y"} - </li>
{paginate}
</ul>
<p><br />
{pagination_links}
{current_page} of {total_pages}
</p>
{/paginate}
{/exp:channel:entries}
该代码显示当前的所有新闻项目。这由最顶部的特色新闻项目和其下方所有先前新闻项目的超链接列表组成。
有没有一种方法可以指定一个变量来存储从最顶部的 select 元素中选择的值,以便我可以将它与下面的新闻项一起使用?或者有更好的方法吗?
In ExpressionEngine I have a news template that displays news items. I am trying to implement a select element that allows the user to select a particular year and then see only news items entered within that year.
Here is my code so far:
<select name="select_year">
<option value="">Select year</option>
<option value="2011">2011</option>
<option value="2010">2010</option>
<option value="2009">2009</option>
</select>
{exp:channel:entries
channel="news-items"
disable="categories|category_fields|trackbacks"
dynamic="off"
require_entry="yes"
sort="desc"
search:news-item-featured="featured"
limit="1"}
<div align="left">
<h2 align="center" style="margin-top: 0pt; margin-bottom: 0pt;">{title}</h2>
</div>
{if news-item-main-image}
{exp:imgsizer:size src="{news-item-main-image}" width="352"}
<p style="margin-top: 0pt;"><img align="middle" width="352" src="{sized}"></p>
{/exp:imgsizer:size}
{/if}
{news-item-intro-text}
<p><a href="{title_permalink='news-item'}">Click here to read the full story. </a></p>
{/exp:channel:entries}
<br><br>
<h3 style="margin-top: 0pt;">News History</h3>
<ul>
{exp:channel:entries
channel="news-items"
disable="categories|category_fields|trackbacks"
dynamic="off"
require_entry="yes"
limit="19"
paginate="bottom"
paginate_base="/news"}
<li><p></p><h3><a href="{title_permalink='news-item'}">{title}</a></h3>
<input type="hidden" value="" name="news_id">{entry_date format="%n/%j/%Y"} - </li>
{paginate}
</ul>
<p><br />
{pagination_links}
{current_page} of {total_pages}
</p>
{/paginate}
{/exp:channel:entries}
The code displays all news items currently. This consists of a featured news item at the very top and a hyperlinked list of all previous news items beneath it.
Is there a way I can specify a variable to store the selected value from the select element at the very top so that I can use it with the news-items below? Or is there perhaps a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有几种方法可以解决这个问题(就像 EE 中的大多数任务一样)。您可以考虑使用 EE 的动态参数功能。 EE 的动态参数允许您的用户从各种选项(包括年份)中进行选择,但它们也会禁用模板的缓存并且不会更新 URL。
您还可以使用 URL 段来控制显示的年份。这比使用动态参数更常见。假设您的新闻索引页位于 http://site.com/news。您可以通过添加第二个 URL 段来触发“存档”功能,如下所示: http://site.com/news /存档。
然后,在模板中,您可以使用简单的条件来触发存档布局的某些代码。使用此方法您的页面将如下所示(重要更改以粗体显示):
请注意,我只是在您的代码中添加了一些
{ifegment_2 == 'archive'}{/if}
条件标记。此外,您可以放弃选择下拉菜单并使用 Low 的免费年度档案插件 对于 EE。该插件为您提供了渲染年度/每月存档链接的很大灵活性。如果您确实想保留选择菜单,则可以在其中添加一些 JavaScript,以使每个选项将用户重定向到正确的存档 URL。
There are a couple of ways to go about this (just like most tasks in EE). You could look into using EE's dynamic parameters feature. EE's dynamic parameters allow your users to select from various options (including year), but they also disable caching for the template and don't update the URL.
You could also use URL segments to control which year is being displayed. This is much more common than using dynamic parameters. Let's say that your News index page lives at http://site.com/news. You could trigger an "archive" feature by adding a second URL segment, like so: http://site.com/news/archive.
Then, in your template you can use simple conditionals to trigger certain code for the archive layout. Here's what your page would look like with this approach (important changes in bold):
Please note that I simply added a few
{if segment_2 == 'archive'}{/if}
conditional tags to your code.Additionally, you could ditch the select dropdown menu and use Low's free Yearly Archives plugin for EE. This plugin gives you lots of flexibility for rendering yearly/monthly archive links. If you really want to keep the select menu, you could add a dash of JavaScript into the mix to have each option redirect the user to the proper archive URL.