前言
发现这类文章很少有人写,大概是大家都会,只有我觉得很厉害吧(
自定义字段
说说其实就是文章,只是在主题的首页显示的时候会显示成“说说”的样式,那么也就是需要在发布文章的时候选择发布说说样式,这里就需要在 functions.php 里增加自定义字段的选择了。 自定义字段是这段代码里的
function themeFields($layout) {
}
然后在里面添加一个选择发布样式的代码
$type=new Typecho_Widget_Helper_Form_Element_Select('type',array('0'=>'一般文章','1'=>'说说'),'0','文章类型');
$layout->addItem($type);
这样就可以在发布时进行选择了,默认选择一般文章。
判定
自定义字段弄完了,需要在首页弄个判定,你是选择的说说还是选择的一般文章,选择哪个就输出哪个的样式 然后这个时候index.php就需要一个if字段
<?php while($this->next()): ?>//循环
<?php if( $this->fields->type=='1' ): ?>
然后if字段底下就要写在type=1的时候输出的内容 比如这段是我给Nexmoe写的内容
<div class="nexmoe-post">
<div class="typebox thebg" >
<h2 style="margin-bottom: 0px;"> 安和说:</h2>
<article>
<span style="font-size:17px;">
<?php $this->content('</span>'); ?>
</article>
<hr><div class="typeafter"> <div class="avatar"> <img src="<?php $this->options->logoUrl();?>" alt="安和" width="50" height="50"> </div> <div class="col-md-11 col-xs-10"> <h4 class="type-title"><a href="<?php $this->permalink() ?>" data-pjax=""><?php $this->title() ?></a></h4>
<div class="saying-info"><div class="nexmoe-post-meta">
<a><i class="nexmoefont icon-calendar-fill"></i><?php $this->date('Y年n月d日');?></a>
<a><i class="nexmoefont icon-areachart"></i><?php artCount($this->cid);?> 汉字</a>
</div> </div>
</div>
</div>
</div>
</div>
<br /><br />
然后输出完这些之后再来个
<?php else: ?>
如果不是type=1就输出默认的文章样式(上文默认选择type0) 这一部分直接粘贴你主题原先输出的部分就可以了 最后
<?php endif; ?>
<?php endwhile; ?>
就大功告成啦
举一反三,例如这个文章需不需要目录树呢,这个文章需不需要灯箱呢,都可以通过自定义字段选择,然后在输出位置用if字段判定即可。