搜索
Hi~登录注册
查看: 1703|回复: 0

wordpress实现不同分类或不同文章调用不同模板的方法

[复制链接]

1892

主题

1899

帖子

6406

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
6406
发表于 2015-5-14 13:49:04 | 显示全部楼层 |阅读模式
本文章要介绍的是两个功能分别是不同分类模板与不同文章调用不同模板了,这篇文章小编是整理了两篇文章,希望对各位会带来帮助.
</div><p><strong>wordpress不同分类调用不同模板文件</strong></p>
<p>在制作wordpress多栏目模板时,遇到不同的分类调用不同模板的情况,假如我的博客有分类PHP技术,ASP技术,每个分类需要调用不同的分类模板,发现一个简单的调用方法,下面就让夏日博客教大家如何调用不同的分类吧。</p>
<p><strong>一、不同分类调用不同模板</strong></p>
<p><br />
&lt;?php&nbsp;&nbsp; <br />
$post = $wp_query-&gt;post;&nbsp; <br />
if ( in_category(&lsquo;7&prime;) ) <br />
{&nbsp;&nbsp; <br />
<a href="/tags.php/include/" target="_blank">include</a>(TEMPLATEPATH . &lsquo;/archive-view.php&rsquo;);&nbsp; }&nbsp;&nbsp; <br />
else if ( in_category(&lsquo;12&prime;) ) <br />
{&nbsp;&nbsp; <br />
include(TEMPLATEPATH . &lsquo;/single12.php&rsquo;);&nbsp; <br />
}&nbsp;&nbsp; <br />
else if ( in_category(&lsquo;42&prime;) ) <br />
{&nbsp;&nbsp; <br />
include(TEMPLATEPATH . &lsquo;/single42.php&rsquo;);&nbsp; <br />
}&nbsp;&nbsp; <br />
else {&nbsp;&nbsp; include(TEMPLATEPATH . &lsquo;/archive-other.php&rsquo;);&nbsp; <br />
}&nbsp; <br />
?&gt;</p>
<p>上面的代码是摘自网络上的一段代码,将代码放入到主题的分类文件archive.php中就可以了,in_category(&rsquo;7&prime;) 中间的数字代码的是分类的ID,这个在后台分类目录中可以看到,而 archive-view.php 则是模板文件,需要在模板里面新建 single-<strong>view.php 文件才可以调用。</strong></p>
<p><strong>二、不同文章按照分类来调用不同模板</strong></p>
<p>在不同的分类中调用了不同的模板,哪么分类下面的文章也需要调用当前分类的模板,看下下面的实现方法,</p>
<p><br />
&lt;?php<br />
$post = $wp_query-&gt;post;<br />
if ( in_category(&lsquo;7&prime;) ) {&nbsp;&nbsp; <br />
include(TEMPLATEPATH . &lsquo;/single-view.php&rsquo;);&nbsp; <br />
}&nbsp;&nbsp; <br />
else if ( in_category(&lsquo;3&prime;)) <br />
{&nbsp;&nbsp; <br />
include(TEMPLATEPATH . &lsquo;/single-case.php&rsquo;);&nbsp; <br />
}&nbsp;&nbsp; <br />
else if ( in_category(&lsquo;42&prime;) ) <br />
{&nbsp;&nbsp; <br />
include(TEMPLATEPATH . &lsquo;/single42.php&rsquo;);&nbsp; <br />
}&nbsp;&nbsp; <br />
else {&nbsp;&nbsp; include(TEMPLATEPATH . &lsquo;/archive-other.php&rsquo;);&nbsp; <br />
}&nbsp; <br />
?&gt;<br />
和 wordpress 不同分类调用不同模板的文件基本一样,就是调用的模板文件 single-view.php 有所不同,这个也同样是需要新建的,这样才可以调用到。当然这段代码也是要放到文章模板页 single.php 中的。</p>
<p><strong>三、首页调用不同的模板。</strong></p>
<p>如果需要首页调用单独的模板,则可以在 archive.php 文件中加入如下的代码则可以实现.</p>
<p><br />
&lt;?php <br />
if (is_home() || is_front_page()) { ?&gt;<br />
include(TEMPLATEPATH . &lsquo;/home.php&rsquo;);&nbsp; <br />
&lt;?php } ?&gt;</p>
<p>如果需要不同的单页面或者是网站标签 tag 也调用单独的模板,则上面的解析代码均可以用在 page 以及 tag 的模板上<strong>面,可以自己尝试着修改一下~~</strong></p>
<p><strong>wordpress不同文章调用不同的模板文件</strong></p>
<p>在文章页面 single.php 中找到如下的代码:<br />
<br />
&lt;?php <br />
get_header(); <br />
?&gt;</p>
<p><br />
将其修改成如下代码:</p>
<p><br />
<br />
&lt;?php <br />
$header = get_post_meta($post-&gt;ID, &quot;header&quot;, true);<br />
get_header($header);<br />
?&gt;</p>
<p><br />
这样在我们发布文章的时候,添加自定义字段 header,自定义字段值为 04ie,添加好后,调用的自定义模板则为 header-04ie.php 了,这个时候我们需要新建一个 header-04ie.php 的头文件来作为模板调用。上面调用更换的为头模板文件,当然侧边栏评论页的模板原理都是一样的。
公众微信:idc5ahl
公众QQ:吾爱互联
关注公众微信,公众QQ每天领现金卡密
卡密介绍(http://www.5ahl.com/thread-2182-1-1.html
回复

使用道具 举报

游客
回复
您需要登录后才可以回帖 登录 | 点我注册

快速回复 返回顶部 返回列表