十七、Wordpress不同分类下文章使用不同的模板

admin2023-01-18989

blob.png

get_template_part()    //自定义模板文件调用函数

single.php    //文章页面模板(包括了文章页面的头部底部侧边栏等等)

content.php    //文章页面内部文章部分模板

content-wpcj.php    //别名为wpcj栏目的文章的文章部分模板

single.php

<? get_header(); ?>
<div class="c">
    <div id="left-box">
        <div id="post-box">
            <?
                the_post();    //获取当前文章内容放入全局变量
                
                $cat = get_the_category( get_the_ID() );    //获取当前文章所在栏目信息
                
                $name = $cat[0]->slug;    //获取所在栏目的别名
                //加载 content-wpcj.php 模版文件,如果文件不存在,则调用content.php
                get_template_part( 'content', $name );
            ?>
        </div>
    </div>
    <? get_sidebar(); ?>
</div>
<? get_footer(); ?>


网友评论