dede arclist current标签如何调用当前栏目文章?

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

Of course! The dede:arclist tag is one of the most frequently used and powerful tags in DedeCMS. The "current" functionality is essential for creating navigation menus, article lists, and other elements where you need to highlight the currently active item.

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

Here’s a comprehensive guide on how to make dede:arclist show the "current" or "active" state.


The Core Concept: Using currentstyle

The primary and most straightforward way to achieve this is by using the currentstyle attribute within the dede:arclist tag. This attribute allows you to define a specific HTML template for the item that matches the current page.

Syntax:

{dede:arclist currentstyle='<li><a href="[field:arcurl/]]" class="current">[field:title/]</a></li>'}
    <li><a href="[field:arcurl/]]" title="[field:title/]">[field:title/]</a></li>
{/dede:arclist}

How it Works:

dede arclist current
(图片来源网络,侵删)
  1. DedeCMS processes the arclist and compares the URL of each list item ([field:arcurl/]) with the URL of the current page.
  2. If a match is found, DedeCMS uses the HTML code defined inside the currentstyle attribute for that specific item.
  3. For all other items, it uses the default template code (the code outside of currentstyle).

Practical Example: Creating a Dynamic Navigation Menu

Let's say you want to create a horizontal menu from a specific channel (e.g., "Products").

Step 1: Create a Channel in DedeCMS Backend

Go to 核心 -> 频道模型 -> 频道管理. Create a new top-level channel, for example, named "Products". Make sure it's set to "使用栏目".

Step 2: Create the Template Code

In your HTML template file (e.g., head.htm), use the following dede:arclist code.

<!-- This example creates a simple navigation menu -->
<ul class="main-nav">
    {dede:arclist typeid='3' currentstyle='<li class="active"><a href="[field:arcurl/]" class="current">[field:title/]</a></li>'}
        <li><a href="[field:arcurl/]" title="[field:title/]">[field:title/]</a></li>
    {/dede:arclist}
</ul>

Explanation of the code:

dede arclist current
(图片来源网络,侵删)
  • typeid='3': This tells arclist to only fetch articles from the channel with ID 3 (our "Products" channel). This is crucial for navigation.
  • currentstyle='...': This is the magic part. If the current page is an article within the "Products" channel, its link will be rendered with the <li class="active">... and <a class="current">...</a> wrappers.
  • The default <li><a href="...">...</a></li> is used for all other items in the list.

Step 3: Add CSS for Styling

Now, add some CSS to make the "current" or "active" link stand out.

.main-nav li {
    display: inline-block;
    margin-right: 10px;
}
.main-nav a {
    text-decoration: none;
    color: #333;
    padding: 5px 10px;
}
.main-nav a.current,
.main-nav li.active a {
    background-color: #007bff;
    color: white;
    border-radius: 4px;
}

Now, when a user is on a page within the "Products" channel, the corresponding link in the menu will be highlighted with a blue background.


Advanced Scenarios and Troubleshooting

Sometimes the basic currentstyle might not work as expected. Here are the common reasons and solutions.

The typeid is Missing or Incorrect

This is the most common mistake. If you don't specify a typeid, arclist will pull articles from all channels, and it's unlikely any will match the current page's URL.

Solution: Always specify the typeid for navigation lists. You can get the ID of your channel from the DedeCMS channel management page.

Mismatch Between List and Page URLs

The currentstyle compares [field:arcurl/] (the URL of the article in the list) with the full URL of the current page. If your site has URL rewriting enabled or uses a different URL structure for list pages vs. article pages, they might not match.

  • List Page URL: /products/ (shows all articles in the "Products" channel)
  • Article URL: /products/a123.html (a specific article)

When you are on the list page /products/, the arclist items have URLs like /products/a123.html. They will never match the list page URL itself.

Solution for Highlighting the List Page Itself: For a navigation menu, you often want the parent channel (the list page) to be highlighted when you are viewing any of its sub-articles OR the list page itself. currentstyle alone cannot do this.

You need to use a combination of PHP and dede:arclist in your template.

Template Code (e.g., head.htm):

<?php
// Get the ID of the current channel
$typename = $GLOBALS['typename'];
$typenametop = $GLOBALS['typenametop'];
$channelid = $GLOBALS['channelid'];
// Check if we are on a list page or an article page within this channel
$isActive = ($channelid == 3); // Change 3 to your target channel ID
?>
<ul class="main-nav">
    {dede:arclist typeid='3'}
        <?php
        // For each item, check if its URL matches the current page OR if we are on the parent channel page
        $currentItemUrl = $fields['arcurl'];
        $currentPageUrl = $_SERVER['REQUEST_URI'];
        $isCurrentItem = ($currentItemUrl == $GLOBALS['cfg_basehost'] . $currentPageUrl || $isActive);
        ?>
        <li<?php if ($isCurrentItem) echo ' class="active"'; ?>>
            <a href="<?php echo $fields['arcurl']; ?>" title="<?php echo $fields['title']; ?>"<?php if ($isCurrentItem) echo ' class="current"'; ?>><?php echo $fields['title']; ?></a>
        </li>
    {/dede:arclist}
</ul>

Note: This PHP-in-template approach can be complex. A simpler alternative is to use a dedicated navigation tag like dede:channelartlist for channel-based menus, which has built-in support for this.

Using dede:channel or dede:channelartlist for Channel Navigation

If your goal is to link to channel list pages (not individual articles), dede:arclist is not the best tool. Use dede:channel or dede:channelartlist instead, as they are designed for this and have simpler "current" logic.

Example with dede:channel: This tag automatically adds the current class to the link of the current channel.

{dede:channel type='top'}
    <li><a href='[field:typelink/]' [field:iscurrent runphp='yes']if(@me == '') @me = ''; else @me = 'class="current"';[/field:iscurrent]>[field:typename/]</a></li>
{/dede:channel}

The [field:iscurrent] field is a boolean that is true for the current channel, making this method very robust for primary navigation.


Summary: Which Method to Use?

Your Goal Recommended Tag Key Attribute/Method
Highlight individual articles in a list dede:arclist currentstyle attribute
Create a main navigation menu for channels dede:channel or dede:channelartlist [field:iscurrent] or built-in currentstyle support
Highlight a channel and all its articles dede:arclist with PHP logic Combine typeid with PHP to check $_SERVER['REQUEST_URI']

For most use cases involving article lists, the currentstyle attribute on dede:arclist is the correct and simplest solution, provided you remember to set the typeid.

-- 展开阅读全文 --
头像
dede缓存listcache是什么?如何清理?
« 上一篇 12-02
dede sql标签如何实现分页?
下一篇 » 12-02

相关文章

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

目录[+]