dede idlist

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

Of course! The dede_idlist parameter is a very common and important variable used in the DedeCMS (织梦内容管理系统) template engine. It's primarily used for custom content queries, allowing you to display specific articles, channels, or other content items based on their IDs.

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

Let's break it down in detail.


What is dede_idlist?

In simple terms, dede_idlist is a variable that holds a comma-separated list of content IDs. When you use it in a template tag, DedeCMS will query the database and fetch only the content items whose IDs are present in that list.

Think of it as a "shopping cart" for content IDs. You tell DedeCMS, "I want articles with ID 5, ID 12, and ID 28," and it will fetch exactly those for you.

How to Use It: The Core Tag

The primary way to use dede_idlist is with the powerful {dede:arclist} tag, which is used to list articles.

Basic Syntax:

{dede:arclist idlist='ID1,ID2,ID3,...' ...}
    <!-- Template items for each article -->
    <a href='[field:arcurl/]'>[field:title/]</a>
    <p>[field:description function='cn_substr(@me,100)'/]...</p>
{/dede:arclist}

The key part here is idlist='...'. You replace the placeholder with the actual IDs you want to retrieve.

Example:

If you want to display articles with IDs 45, 67, and 89:

<h2>精选文章</h2>
<ul>
    {dede:arclist idlist='45,67,89' titlelen='30' row='3'}
        <li>
            <a href="[field:arcurl/]" title="[field:title/]">[field:title/]</a>
        </li>
    {dede:arclist}
</ul>

This code will:

  1. Query the dede_archives table.
  2. Find the articles where id is 45, 67, or 89.
  3. Display the titles of up to 3 (row='3') of these articles as links.
  4. The titlelen='30' parameter will trim the title to a maximum of 30 characters.

How to Dynamically Set idlist (The Most Common Use Case)

Hardcoding IDs like 45,67,89 is not very flexible. The real power of dede_idlist comes when you set it dynamically. This is often done using PHP.

Method 1: Using a URL Parameter (e.g., ?id=45,67,89)

You can pass the list of IDs directly in the URL and then read it in your template using PHP.

Template File (e.g., list_special.htm):

{dede:php}
    // Get the 'id' parameter from the URL
    $idlist = $_GET['id'];
    // IMPORTANT: Always validate and sanitize user input to prevent SQL injection!
    // A simple check is to ensure it contains only numbers and commas.
    if (preg_match('/^[\d,]+$/', $idlist)) {
        // Assign the value to the DedeCMS variable
        $GLOBALS['idlist'] = $idlist;
    } else {
        // If the input is invalid, you can either show nothing or a default list
        $GLOBALS['idlist'] = ''; 
    }
{/dede:php}
{if $idlist != ''}
    <h2>您选择的文章</h2>
    {dede:arclist idlist='$idlist' row='10'}
        <a href="[field:arcurl/]">[field:title/]</a> <span style="color:#999">([field:pubdate function='MyDate('Y-m-d', @me)'/])</span><br />
    {/dede:arclist}
{else}
    <p>无效的文章ID。</p>
{/if}

Now, if you visit the page like this: http://yourdomain.com/plus/list.php?tid=2&id=45,67,89 The page will display the articles with IDs 45, 67, and 89.

Method 2: Setting IDs from a PHP File (e.g., for a special page)

You can create a PHP file that sets the idlist variable and then includes the DedeCMS template engine to parse the template.

PHP File (e.g., special_page.php):

<?php
require_once(dirname(__FILE__)."/include/common.inc.php");
// Define the list of IDs you want to show
$special_ids = array(101, 102, 103, 104);
$idlist = implode(',', $special_ids);
// Assign the ID list to a global variable for the template
$GLOBALS['idlist'] = $idlist;
// Get the HTML output from the template file
$tplfile = DEDETEMPLATE.'/default/special_page.htm'; // Path to your template
$dtp = new DedeTemplate();
$dtp->LoadTemplate($tplfile);
$dtp->Display();
?>

Template File (special_page.htm):

{dede:arclist idlist='$idlist' row='20' titlelen='50'}
    <div class="article-item">
        <h3><a href="[field:arcurl/]">[field:title/]</a></h3>
        <p>[field:description function='cn_substr(@me,150)'/]...</p>
    </div>
{/dede:arclist}

Key Parameters to Use with idlist

When using idlist, you often combine it with other {dede:arclist} parameters to control the output:

  • row='N': The number of articles to display. If you have 5 IDs in idlist but set row='3', only 3 will be shown.len='N'`: The length of the article title.
  • infolen='N': The length of the article summary (description).
  • orderby='id': Sorts the results. Common values are id (default), pubdate (publication date), hot (click count), etc.
  • orderway='asc' or desc: Ascending or descending order. Often used with orderby.

Example with ordering:

<!-- Display 5 articles from the ID list, ordered by publication date, newest first -->
{dede:arclist idlist='45,67,89' row='5' orderby='pubdate' orderway='desc'}
    [field:pubdate function='MyDate('Y-m-d H:i', @me)'] - [field:title/]<br />
{/dede:arclist}

Important Security Consideration

Whenever you use user-provided data (like from a URL $_GET) to build a database query, you must validate and sanitize it to prevent SQL injection attacks.

The code preg_match('/^[\d,]+$/', $idlist) shown in the dynamic example is a basic but effective way to do this. It ensures that the $idlist variable only contains numbers and commas, which is the only valid format for an ID list. This prevents a malicious user from injecting harmful SQL code.

Summary

Feature Description
What it is A DedeCMS template variable for a comma-separated list of content IDs.
Main Use With {dede:arclist} to fetch specific articles by their ID.
Syntax {dede:arclist idlist='1,5,10' ...}
Dynamic Use Set via PHP in the template ({dede:php} $GLOBALS['idlist'] = ...; {/dede:php}) or passed via a URL parameter.
Security Always validate/sanitize dynamic ID lists to prevent SQL injection.
Flexibility Extremely useful for "featured articles," "editor's picks," or any curated content list.
-- 展开阅读全文 --
头像
dede fileid是什么?如何使用?
« 上一篇 03-29
织梦搜索页面调用代码
下一篇 » 03-29

相关文章

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

目录[+]