dede php addfilter是什么?如何使用?

99ANYc3cd6
预计阅读时长 24 分钟
位置: 首页 DEDE建站 正文

Of course. The term dede php addfilter is very specific to the DedeCMS (织梦CMS) ecosystem. Let's break it down in detail.

dede php addfilter
(图片来源网络,侵删)

What is addfilter in DedeCMS?

In DedeCMS, addfilter is a PHP function used within the list template (the template for listing articles, like list_article.htm) to dynamically generate a URL for filtering or sorting content.

The most common use case is for creating links that sort articles by a specific field, such as:

  • Publish Date (发布日期)
  • Click Count (点击量)
  • Sticky Level (置顶级别)
  • Weight/Order (权重/排序)

The function is typically used in the context of the DedeCMS tag {dede:list}.


Syntax and Parameters

The addfilter function has a specific structure. It's not a standard PHP function you call directly but rather a special tag understood by the DedeCMS engine.

dede php addfilter
(图片来源网络,侵删)

Basic Syntax:

<a href="{dede:field name='phpurl'/}/plus/list.php?tid={dede:field name='id'/}&{dede:addfilter field='排序字段' type='排序方式'/}">显示文字</a>

Let's break down the {dede:addfilter} tag itself:

{dede:addfilter field='字段名' type='排序方式'/}

  • field (必需参数): Specifies the database field you want to sort by.

    dede php addfilter
    (图片来源网络,侵删)
    • Common values:
      • sortrank: Sort order / Weight (权重)
      • pubdate: Publication date (发布日期)
      • click: Click count (点击量)
      • lastpost: Last post time (最后回复时间)
      • mid: Author ID (作者ID)
  • type (必需参数): Specifies the direction of the sort.

    • Common values:
      • asc: Ascending order (升序, e.g., A-Z, 1-9, old to new)
      • desc: Descending order (降序, e.g., Z-A, 9-1, new to old)

How It Works: A Practical Example

Let's imagine you have a category page (list_article.htm) and you want to add links to let users sort the articles by date (newest first) or by popularity (most clicks first).

Your list_article.htm template might look like this:

{dede:list pagesize='10'}
    <li>
        <a href="[field:arcurl/]">[field:title/]</a>
        <span>[field:pubdate function="MyDate('Y-m-d', @me)"/] | [field:click/] clicks</span>
    </li>
{/dede:list}
<!-- 分页 -->
{dede:pagelist listitem="info,index,end,pre,next,pageno" listsize="5"/}
<!-- 在分页代码下方,添加排序链接 -->
<div class="sort-links">
    <strong>Sort by:</strong>
    <a href="{dede:field name='phpurl'/}/plus/list.php?tid={dede:field name='id'/}&{dede:addfilter field='pubdate' type='desc'/}">Newest First</a>
    <a href="{dede:field name='phpurl'/}/plus/list.php?tid={dede:field name='id'/}&{dede:addfilter field='click' type='desc'/}">Most Popular</a>
    <a href="{dede:field name='phpurl'/}/plus/list.php?tid={dede:field name='id'/}&{dede:addfilter field='sortrank' type='asc'/}">Manual Order</a>
</div>

Explanation of the Example:

  1. {dede:field name='phpurl'/}: This gets the base URL to the php directory, which is where list.php is located.
  2. {dede:field name='id'/}: This gets the current category ID (tid). This is crucial for ensuring the filter applies only to the articles in the current category.
  3. &{dede:addfilter field='pubdate' type='desc'/}: This is the core of the function.
    • When clicked, DedeCMS's list.php script will see this parameter.
    • It will automatically generate a query string that looks like this: ?tid=123&orderby=pubdate&orderway=desc.
    • The list.php script then uses these parameters to modify the database query, fetching articles sorted by pubdate in descending order.
  4. Resulting URLs:
    • "Newest First" link might generate: .../list.php?tid=5&orderby=pubdate&orderway=desc
    • "Most Popular" link might generate: .../list.php?tid=5&orderby=click&orderway=desc

Important Considerations and Troubleshooting

  • Template Engine: Remember that this is not raw PHP. It's a template tag processed by DedeCMS's template engine. You cannot use it outside of DedeCMS template files (.htm).
  • {dede:list} Context: The addfilter tag is almost always used in conjunction with {dede:list} to provide alternative views of the same list of articles.
  • SEO Impact: Be aware that creating many filtered pages (e.g., sorting by every possible field) can lead to content duplication from an SEO perspective. Search engines might see ?tid=5&orderby=date and ?tid=5&orderby=click as different pages with very similar content. Use this feature judiciously.
  • Not Working? If addfilter doesn't produce a link:
    1. Check Typos: Ensure the field and type values exactly match a database column and a valid sort direction.
    2. Check Permissions: Make sure your list.php file in /plus/ is executable and has the correct permissions.
    3. Check DedeCMS Version: While this feature has been around for a long time, ensure your version of DedeCMS supports it. It's a standard feature in most versions from DedeCMS 5.6 onwards.

Modern Alternatives

While addfilter is the "classic" DedeCMS way, modern web development often favors a more robust approach:

  • AJAX Filtering: Instead of reloading the entire page, you could use JavaScript (with jQuery or Fetch API) to send the sorting parameters to the server in the background and update the article list dynamically. This provides a much better user experience.
  • Custom PHP/MySQL: For more complex filtering (e.g., filtering by multiple tags, custom fields, or date ranges), you might write your own PHP controller and SQL query, giving you full control over the logic.

Summary

Term Meaning Use Case
dede Refers to the DedeCMS content management system. Context for the entire question.
php Indicates that the solution involves PHP code or DedeCMS's PHP-based templating. The logic is handled by PHP files like list.php.
addfilter A DedeCMS template tag that dynamically generates a URL with sorting/filtering parameters. Creating "Sort by Date," "Sort by Popularity" links on a category page.

In short, dede php addfilter is the DedeCMS method for creating dynamic sorting links on a list page. It's a convenient, built-in feature that leverages the system's existing list.php script to handle the database sorting logic.

-- 展开阅读全文 --
头像
C语言Stack Overflow如何排查与解决?
« 上一篇 2025-12-12
C语言在Linux与Windows系统开发中的差异是什么?
下一篇 » 2025-12-12

相关文章

取消
微信二维码
支付宝二维码

目录[+]