日志详情页模板调用有两种方法,一种是分类不同日志详情页相同,另一种是分类不同日志详情页不同。
第一种方式调用的代码如下,直接在single.php文件写入类似如下程序
<?php the_post(); ?>
<div class="col-md-8">
<h1><?php the_title(); ?></h1>
<div class="post-info">
<span class="post-date"><?php the_time('Y-m-d'); ?></span> /
<span class="post-author"> by <?php the_author(); ?></span> /
<span class="cat-permalink"><?php the_category('|'); ?></span>
</div>
<div class="post-content">
<p><?php the_content(); ?></p>
</div>
<ul class="prenext">
<li class="post-pre">上一篇:<?php previous_post_link() ?></li>
<li class="post-next">下一篇:<?php next_post_link() ?></li>
</ul>
</div>
第二种方式调用的代码参见:content.php模板文件
the_post() //获取日志信息
the_title() //获取当前日志的标题
the_permalink() //获取当前日志的链接
the_content() //获取当前日志的文章内容
the_author() //当前文章的作者
_e() //获取翻译并且输出
__() //获取翻译并且返回值
the_category() //当前文章所属的分类
edit_post_link() //当登录管理员或有权限的编辑当前文章的时候显示一个编辑链接,方便修改文章
previous_post_link() //上一篇文章链接
next_post_link() //下一篇文章链接
content.php模板文件
此模板建立是为了满足不同分类调用不同日志详情页的需求,方法如下
一、首先在single.php文件写下如下代码
<div class="left-box">
<?php
the_post();
$cat = get_the_category($post->ID);
$name = $cat[0]->slug; //调用分类别名
get_template_part('content',$name); //获得content-分类别名.php文件
?>
</div>
get_the_category() //注:获取当前文章的对应的分类
用法
get_the_category(get_the_ID())或get_the_category($post->ID)
get_template_part() //注:自定义模板文件调用函数
二、在content.php模板文件和content-分类别名.php模板文件写入日志详情模板代码,如果存在content-分类别名.php,则相应的分类的日志详情页会调用content-分类别名.php模板,如果不存在content-分类别名.php模板文件,则日志详情页模板调用content.php模板文件
转载请注明:落伍老站长 » wp主题开发single.php模板文件