Zblog php calls random articles, popular articles, hot article programs

Source: Internet
Author: User
Tags foreach auth comments datetime php file rand
Usage:

 

First, we need to create the include. Php file in our topic directory. If so, add the script directly.

 

The code is as follows: Copy code

/**
* Get the article list
* @ Param int $ count quantity
* @ Param null $ cate Category ID
* @ Param null $ auth user ID
* @ Param null $ date
* @ Param null $ tags tag
* @ Param null $ search keywords
* @ Param null $ order sorting
* @ Param null $ option
* @ Return array | mixed
*/
Function TcgetList ($ count = 10, $ cate = null, $ auth = null, $ date = null, $ tags = null, $ search = null, $ option = null, $ order = null ){
Global $ zbp;

If (! Is_array ($ option )){
$ Option = array ();
}

If (! Isset ($ option ['only _ ontop '])
$ Option ['only _ ontop '] = false;
If (! Isset ($ option ['only _ not_ontop '])
$ Option ['only _ not_ontop '] = false;
If (! Isset ($ option ['has _ subcate '])
$ Option ['has _ subcate'] = false;
If (! Isset ($ option ['is _ related'])
$ Option ['is _ related'] = false;

If ($ option ['is _ related']) {
$ At = $ zbp-> GetPostByID ($ option ['is _ related']);
$ Tags = $ at-> Tags;
If (! $ Tags)
Return array ();
$ Count = $ count + 1;
}

If ($ option ['only _ ontop '] = true ){
$ W [] = array ('=', 'log _ IsTop ', 0 );
} Elseif ($ option ['only _ not_ontop '] = true ){
$ W [] = array ('=', 'log _ IsTop ', 1 );
}

$ W = array ();
$ W [] = array ('=', 'log _ status', 0 );

$ Articles = array ();

If (! Is_null ($ cate )){
$ Category = new Category;
$ Category = $ zbp-> GetCategoryByID ($ cate );

If ($ category-> ID> 0 ){

If (! $ Option ['has _ subcate']) {
$ W [] = array ('=', 'log _ cateid', $ category-> ID );
} Else {
$ Arysubcate = array ();
$ Arysubcate [] = array ('log _ cateid', $ category-> ID );
Foreach ($ zbp-> categorys [$ category-> ID]-> SubCategorys as $ subcate ){
$ Arysubcate [] = array ('log _ cateid', $ subcate-> ID );
}
$ W [] = array ('array', $ arysubcate );

}

}
}

If (! Is_null ($ auth )){
$ Author = new Member;
$ Author = $ zbp-> GetMemberByID ($ auth );

If ($ author-> ID> 0 ){
$ W [] = array ('=', 'log _ authorid', $ author-> ID );
}
}

If (! Is_null ($ date )){
$ Datetime = strtotime ($ date );
If ($ datetime ){
$ Datetitle = str_replace (array ('% y %', '% m %'), array (date ('Y', $ datetime), date ('N ', $ datetime), $ zbp-> lang ['MSG '] ['Year _ month']);
$ W [] = array ('between', 'log _ posttime', $ datetime, strtotime ('+ 1 month', $ datetime ));
}
}

If (! Is_null ($ tags )){
$ Tag = new Tag;
If (is_array ($ tags )){
$ Ta = array ();
Foreach ($ tags as $ t ){
$ Ta [] = array ('log _ tag', '% {'. $ t-> ID. '} % ');
}
$ W [] = array ('Array _ like', $ ta );
Unset ($ ta );
} Else {
If (is_int ($ tags )){
$ Tag = $ zbp-> GetTagByID ($ tags );
} Else {
$ Tag = $ zbp-> GetTagByAliasOrName ($ tags );
}
If ($ tag-> ID> 0 ){
$ W [] = array ('like', 'log _ tag', '% {'. $ Tag-> ID. '} % ');
}
}
}

If (is_string ($ search )){
$ Search = trim ($ search );
If ($ search! = ''){
$ W [] = array ('search', 'log _ content', 'log _ intro', 'log _ title', $ search );
}
}

If (! Empty ($ order )){
If ($ order = 'new '){
$ Order = array ('log _ posttime' => 'desc ');
}
If ($ order = 'Hot '){
$ Order = array ('log _ viewnums' => 'desc ');
}
If ($ order = 'comm '){
$ Order = array ('log _ commnums' => 'desc ');
}
If ($ order = 'Rand '){
$ Order = array ('rand () '=> '');
}
}

$ Articles = $ zbp-> GetArticleList ('*', $ w, $ order, $ count, null, false );

If ($ option ['is _ related']) {
Foreach ($ articles as $ k => $ ){
If ($ a-> ID = $ option ['is _ related'])
Unset ($ articles [$ k]);
}
If (count ($ articles) = $ count ){
Array_pop ($ articles );
}
}

Return $ articles;

}

 

Then it is called in the interface template we need.

 

A-random article

 

The code is as follows: Copy code

{$ Array = TcgetList (10, null, 'Rand ');}
<Ul id = "related">
{Foreach $ array as $ related}
<Li> <span class = "time"> {$ related. time ('M-D')} </span> <span class = "title"> <a href = "{$ related. url} "title =" {$ related. title} "> {$ related. title }</a> </span> </li>
{/Foreach}
</Ul>

 

10 random articles

 

B-Popular articles

 

The code is as follows: Copy code

{$ Array = TcgetList (10, null, 'Hot ');}
<Ul id = "related">
{Foreach $ array as $ related}
<Li> <span class = "time"> {$ related. time ('M-D')} </span> <span class = "title"> <a href = "{$ related. url} "title =" {$ related. title} "> {$ related. title }</a> </span> </li>
{/Foreach}
</Ul>

 

Call 10 popular articles

 

C-hot comments

 

The code is as follows: Copy code

{$ Array = TcgetList (10, null, 'comm ';}
<Ul id = "related">
{Foreach $ array as $ related}
<Li> <span class = "time"> {$ related. time ('M-D')} </span> <span class = "title"> <a href = "{$ related. url} "title =" {$ related. title} "> {$ related. title }</a> </span> </li>
{/Foreach}
</Ul>

 

Call 10 hot comments.

 

You can call the API based on actual usage.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.