DTCMS自定义标签,获取所有栏目文章列表TOP,以及文章通用URL

时间:2021-10-11 00:11:42

网站最近更新等地方,发现DTCMS没有获取所有栏目文章列表的标签,只能自己写

思路:获取所有栏目文章列表不难,难点在于linkurl的写法

1、制作获取所有文章列表标签

DTcms.Web.UI\Label\article.cs中

  /// <summary>
/// 自定义:文章列表
/// </summary>
/// <param name="top">显示条数</param>
/// <param name="strwhere">查询条件</param>
/// <returns>DataTable</returns>
protected DataTable get_article_list(int top, string strwhere)
{
return new BLL.article().GetList(top, strwhere, " sort_id asc,add_time desc").Tables[];
}

2、制作linkurl,首先需要获得文章详细页的调用名称,比如furniture_show,组成linkurl("furniture_show",{dr[id]})。{dr[id]}为文章ID,

这里需要读取xmlconfig\urls.config 节点

DTcms.Web.UI\Label\category.cs中 增加两个标签

 /// <summary>
/// 自定义:根据栏目的别名 返回URL名称
/// </summary>
/// <param name="category_name">栏目的别名</param>
/// <returns>String</returns>
protected string get_url_name(string category_name, string attrType)
{
string urlchannel = string.Empty;
BLL.url_rewrite bll = new BLL.url_rewrite();
if (!string.IsNullOrEmpty(category_name))
{
urlchannel = bll.GetInfo(category_name, attrType).name;
}
return urlchannel;
}
/// <summary>
/// 自定义:返回当前类别调用别名
/// </summary>
/// <param name="category_id">类别ID</param>
/// <returns>String</returns>
protected string get_category_name(string category_id)
{
string default_value = string.Empty;
BLL.channel bll = new BLL.channel();
if (bll.Exists(Convert.ToInt32(category_id)))
{
return bll.GetModel(Convert.ToInt32(category_id)).name;
}
return default_value;
}

3、在index.html模版中

   <%set DataTable redNews=get_article_list(1, "status=0 and is_red=1 and img_url<>''")%>
<%foreach(DataRow dr in redNews.Rows)%>
<li>
<a title="{dr[title]}" href="<%linkurl(get_url_name(get_category_name({dr[channel_id]}),"detail"),{dr[id]})%>">
<span class="note-bg"></span>
<span class="note-txt">{dr[title]}</span>
<img src="{dr[img_url]}" />
</a>
</li>
<%/foreach%>
   <%set DataTable newest=get_article_list(10, "status=0 ")%>
<%foreach(DataRow dr in newest.Rows)%>
<li><span><%datetostr({dr[add_time]},"yyyy-MM-dd")%></span><a title="{dr[title]}" href="<%linkurl(get_url_name(get_category_name({dr[channel_id]}),"detail"),{dr[id]})%>">
<%cutstring({dr[title]},35)%></a></li>
<%/foreach%>