Z-Blog主页
Z-Wiki
应用中心
菠萝阁
ZBlogger
Z5加密
跳至内容
Z-Blog Wiki
Z-Blog官方文库
用户工具
登录
站点工具
搜索
工具
显示页面
修订记录
最近更改
媒体管理器
网站地图
登录
>
最近更改
媒体管理器
网站地图
您在这里:
Z-Blog & Z-BlogPHP
»
Z-BlogPHP
»
应用开发指南
»
主题
»
Z-BlogPHP 主题制作技巧
zblogphp:development:theme:tips
本页面只读。您可以查看源文件,但不能更改它。如果您觉得这是系统错误,请联系管理员。
====== Z-BlogPHP 主题制作技巧====== <note important>注意,本页内容为开发者提供的实现实例,请在理解代码后再使用。</note> ===== 同分类文章列表调用===== <code php> {foreach GetList(调用条数,分类ID) as $related} <li><span>{$related.Time('Y-m-d')}</span><a href="{$related.Url}">{$related.Title}</a></li> {/foreach} </code> 获取大目录下的所有文章(包括子目录文章),将上面的GetList改为:<code php>Getlist(调用条数,分类ID,null,null,null,null,array('has_subcate'=>true));</code> 调用其它数据参考文章标签,[[template#文章标签_含独立页面|文章标签]]。\\ **注意**:此处需要使用''foreach''循环中as后面变量名,如案列中使用的$related,如需调用标题则用 ''{$related.Title}'',而并非是'' {$article.Title}''。 ===== 网站关键词、描述添加===== <code php> {if $type=='article'} <title>{$title}_{$article.Category.Name}_{$name}</title> {php} $keywords = $article->TagsName; $description = preg_replace('/[\r\n\s]+/', ' ', trim(SubStrUTF8(TransferHTML($article->Content,'[nohtml]'),135)).'...'); {/php} <meta name="keywords" content="{$keywords}"/> <meta name="description" content="{$description}"/> <meta name="author" content="{$article.Author.StaticName}"> {elseif $type=='page'} <title>{$title}_{$name}_{$subname}</title> <meta name="keywords" content="{$title},{$name}"/> {php} $description = preg_replace('/[\r\n\s]+/', ' ', trim(SubStrUTF8(TransferHTML($article->Content,'[nohtml]'),135)).'...'); {/php} <meta name="description" content="{$description}"/> <meta name="author" content="{$article.Author.StaticName}"> {elseif $type=='index'} <title>{$name}{if $page>'1'}_第{$pagebar.PageNow}页{/if}_{$subname}</title> <meta name="Keywords" content="自定义关键词,{$name}"> <meta name="description" content="自定义描述_{$name}_{$title}"> <meta name="author" content="{$zbp.members[1].StaticName}"> {else} <title>{$title}_{$name}_第{$pagebar.PageNow}页</title> <meta name="Keywords" content="{$title},{$name}"> <meta name="description" content="{$title}_{$name}_当前是第{$pagebar.PageNow}页"> <meta name="author" content="{$zbp.members[1].StaticName}"> {/if} </code> ===== 评论数判断 ===== <code php> {if $article.CommNums==0} 暂无留言 {elseif $article.CommNums==1} 仅有1条留言 {else} 已有{$article.CommNums}条留言 {/if} </code> ===== 页面判断 ===== <code php> {if $type=='index'&&$page=='1'} /*判断首页*/ {if $type=='category'} /*判断分类页*/ {if $type=='article'} /*判断日志页,不含独立页面,{if $article.Type==ZC_POST_TYPE_ARTICLE}(另一方案)*/ {if $type=='page'} /*判断独立页面*/ {if $type=='author'} /*判断用户页*/ {if $type=='date'} /*判断日期页*/ {if $type=='tag'} /*判断标签页*/ </code> ==== 示例:首页和分类列表页分离 ==== 在index.php文件里作判断,分离模板。比如: <code php> {if $type=='index'&&$page=='1'} {template:c_index} {else} {template:c_list} {/if} </code> 然后新建两个相应的模板文件:c_index.php和c_list.php ===== 随机获得文章中的四张图片中的一张 ===== 适用于多图站点,在模板文件中使用: <code php> {php} $temp=mt_rand(1,4); $pattern="/<img[^>]*src=\"([^\"]+\.(gif|jpg|png))\"[^>]*>/"; $content = $article->Content; preg_match_all($pattern,$content,$matchContent); if(isset($matchContent[1][0])) $temp=$matchContent[1][0]; else $temp=$zbp->host."zb_users/theme/$theme/style/images/random/$temp.jpg"; //需要在相应位置放置4张jpg的文件,名称为1,2,3,4 {/php} <img src="{$temp}" /> </code> ===== 仅显示文字的文章简介调用方法 ===== <code php> {SubStrUTF8(TransferHTML($article.Intro,"[nohtml]"),200)} </code> ===== 分类目录面包屑的代码编写 ===== <code php> {php} $html=''; function navcate($id){ global $html; $cate = new Category; $cate->LoadInfoByID($id); $html ='>> <a href="' .$cate->Url.'" title="查看' .$cate->Name. '中的全部文章">' .$cate->Name. '</a> '.$html; if(($cate->ParentID)>0){navcate($cate->ParentID);} } navcate($article->Category->ID); global $html; echo $html; {/php} </code> ===== 显示文章的相关文章 ===== 方法一和方法二的区别:方法一是通过匹配tags,然后按照更新时间列出相关文章;方法二,同样通过匹配tags,然后随机列出相关,但随机比较消耗资源,并且不支持SQLite数据库。 ==== 方法1 ==== 使用getlist,请参考**GetList 函数 [[zblogphp:development:functions:getlist#例5]]** ===== 友好的时间显示 ===== <code php> function TimeAgo( $ptime ) { $ptime = strtotime($ptime); $etime = time() - $ptime; if($etime < 1) return '刚刚'; $interval = array ( 12 * 30 * 24 * 60 * 60 => '年前 ('.date('Y-m-d', $ptime).')', 30 * 24 * 60 * 60 => '个月前 ('.date('m-d', $ptime).')', 7 * 24 * 60 * 60 => '周前 ('.date('m-d', $ptime).')', 24 * 60 * 60 => '天前', 60 * 60 => '小时前', 60 => '分钟前', 1 => '秒前' ); foreach ($interval as $secs => $str) { $d = $etime / $secs; if ($d >= 1) { $r = round($d); return $r . $str; } }; } </code> **友好时间在编译模板文件中使用方法**\\ {TimeAgo($**XXX**.Time())}\\ 例:{TimeAgo($article.Time())} ===== 判断是否为手机端 ===== <code php> function APPID_is_mobile() { if ( empty($_SERVER['HTTP_USER_AGENT']) ) { $is_mobile = false; } elseif ( strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile') !== false // many mobile devices (all iPhone, iPad, etc.) || strpos($_SERVER['HTTP_USER_AGENT'], 'Android') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'Silk/') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'Kindle') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'BlackBerry') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mini') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mobi') !== false ) { $is_mobile = true; } else { $is_mobile = false; } return $is_mobile; } </code> 将以上函数保存到主题include.php **在模板文件中使用方法**\\ {if APPID_is_mobile()} 手机端代码 {else} PC端代码 {/if}
zblogphp/development/theme/tips.txt
· 最后更改: 2022/02/06 13:27 由
唐朝
页面工具
显示页面
修订记录
页面重命名
回到顶部