Blog > 互联网产品设计 > LBS文章页添加随机文章模块

LBS文章页添加随机文章模块

  LBS下默认是没有随机选择文章的。网上已经有很多大侠添加了此项模块。将其放到了sidebar的下面。不过据长期观察,Sidebar除了RSS订阅的图标,其他模块多为文字。放到一起可阅读性并不是太好,自然点击的人也不会太多。于是我将这个随机文章模块添加到了文章详细页面中的右边。也是很多网站公认比较合适用来放置广告的位置,这样也能更好的为站点进行文章推荐。效果如图:

uploads/200706/17_221458_1.jpg

下面我们进行代码修改:
1、打开如下页面Class/Cache.asp,添加一个新数组;

this.randomArticles = new Array();

2、找到如下两端代码位置;

// Load All Caches ————————–

// Load All From DB ————————–

在两处最下方添加

this.loadRandomArticles();

3、然后添加下面的方法;

// Load articles randomly ———————–
this.loadRandomArticles = function()
{
// Get Max article Id;
this.randomArticles = new Array();
var tmpA = connBlog.query(“SELECT TOP 1 log_id FROM [blog_Article] ORDER BY log_id DESC”,undefined,undefined,true);
if(tmpA != null)
{        var maxId = tmpA.getItem(0,0);
        delete tmpA;
        for(var i = 0; i < 10; i++)
        {
        
// Get article id ramdonly;
var rId;
while(rId = this.rand(maxId))
{        if(!this.isInRandomArray(rId))
        break;
}
tmpA = connBlog.query(“SELECT log_title, log_viewCount FROM [blog_Article] WHERE log_id=” + rId,undefined,undefined,true);
if(tmpA != null)
{        this.randomArticles[i] = {“id”: rId,
        ”title”: tmpA.getItem(0,0),
        ”count”: tmpA.getItem(1,0)
};
}else i–;
delete tmpA;
} }
delete tmpA;
}
this.isInRandomArray = function(number)
{        for(var i = 0; i < this.randomArticles.length; i++)
{        with(this.randomArticles[i])
{        if(id == number)
        return true;
} }
return false;
}
this.rand = function(number){
var r = Math.ceil(number * Math.random() + 1);
return r > number ? number : r;
}

4、打开css文件,添加如下代码(具体颜色样式请自行修改);

.randomArticle{float:right; margin:10px 0 10px 5px; font-size:12px; background:#F7FFFf; border:1px solid #23434E; border-top:3px solid #23434E; padding:8px 3px 3px 3px;}
.randomArticle li{list-style:none; padding:0px; margin:0px;}
.randomArticle ul{padding:0px; margin:0px;}
.randomArticle h5{padding:0px 6px 0 6px;margin-top:-25px;float:left;    position:absolute;color: #116093;font-size: 14px;font-weight: bold;color:#333;background:#F7FFFf;}

5、打开article.asp文件,搜索如下代码;

<div id=”textboxContent” class=”textbox-content”>

添加如下代码

<div id=”panelCategory” class=”randomArticle”>
  <h5>Continue Reading…</h5>
  <ul>
   <%for(var i=0;i<theCache.randomArticles.length;i++){
with(theCache.randomArticles[i]){ %>
<li>[<%=count%>] <a href=”article.asp?id=<%=encodeURIComponent(id)%>” mce_href=”article.asp?id=<%=encodeURIComponent(id)%>” title=”<%=func.HTMLEncode(title)%>”><%=func.HTMLEncodeLite(func.cutString(title,24))%></a> </li>
<% } }%>
  </ul>
</div>

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>