zoukankan      html  css  js  c++  java
  • B:Wordpress不同分类调用不同的模板

    这里指的是默认文章类型的模板(single.php,category.php)

    应用场景:

    默认文章默认有2个大类(新闻资讯、游戏资料)

    新闻资讯下的所有子分类调用“新闻资讯列表模板新闻内容模板

    游戏资料下的所有子分类调用“游戏资料列表模板游戏资料内容模板

     文章列表页category.php

    在category.php做判断

    如果该子分类属于“新闻资讯根分类”,则调用新闻资讯列表模板

    如果该子分类属于“游戏资料根分类”,则调用游戏资料列表模板

    这里的关键是“判断子分类是否属于根分类的函数

    Wordpress没有默认的函数,需要如下代码:

    //函数cate_is_in_descendant_category( $cats )
    //参数$cats一个分类ID,多个分类用ID数组

    if
    ( ! function_exists( "post_is_is_descendant_category" ) ) { function cate_is_in_descendant_category( $cats ) { foreach ( (array) $cats as $cat ) { // get_term_children() accepts integer ID only $descendants = get_term_children( (int) $cat, "category" ); if ( $descendants && is_category( $descendants ) ) return true; } return false; } }

    is_category( $category )
    参数:$category

    (混合) (可选) 分类 ID, 分类标题 Title, 分类短标记 Slug 或者 ID数组, Title数组, slugs数组.
    默认: None

    实现操作

    首先,复制两个category.php文件分别取名为“category1.php” 和“category2.php”。
    然后,把原先的category.php文件里面的内容全部删除,并用下面的代码进行替换:

    <?php
    if ( cate_is_in_descendant_category( 2 ) ) {
        include(TEMPLATEPATH . '/category1.php');
    } else {
        include(TEMPLATEPATH . '/category2.php');
    }
    ?>


    意思是:检查分类页ID,如果该ID属于分类ID9,则显示category1.php,如果不是,则显示category2.php。

     文章列表页category.php

    在single.php做判断 

    这里的关键是“判断子分类下的文章是否属于根分类的函数

    Wordpress没有默认的函数,需要如下代码:

    if ( ! function_exists( "post_is_in_descendant_category" ) ) {
        function post_is_in_descendant_category( $cats, $_post = null ) {
            foreach ( (array) $cats as $cat ) {
                // get_term_children() accepts integer ID only
                $descendants = get_term_children( (int) $cat, "category" );
                if ( $descendants && in_category( $descendants, $_post ) )
                    return true;
                }
                return false;
            }
    }

    in_category(  $category , $_post )
    参数1:$category

    (混合的)(必选的)一个或多个被指定分类ID,分类别名或slug,或一个数组。

    默认: 无

    参数2:$_post

    (混合的)(可选的)文章,默认为在主循环内的当前文章或在主查询中的文章。

    默认: 无

    实现操作

    首先,复制两个single.php文件分别取名为“single1.php” 和“single2.php”。
    然后,把原先的single.php文件里面的内容全部删除,并用下面的代码进行替换:

    <?php
    if ( cate_is_in_descendant_category( 2 ) ) {
        include(TEMPLATEPATH . '/single1.php');
    } else {
        include(TEMPLATEPATH . '/single2.php');
    }
    ?>


    意思是:检查日志,如果日志属于分类ID9,则显示single1.php,如果不是,则显示single2.php。

  • 相关阅读:
    redis-hash
    redis-list操作
    bootstrap之消息提示
    jQuery水平下拉菜单实现
    JavaScript的Date对象
    积水问题
    Queue的push和front操作
    Stack的pop和push操作
    .py文件不能设置默认打开程序 win10
    Anaconda的安装
  • 原文地址:https://www.cnblogs.com/roluce/p/6050426.html
Copyright © 2011-2022 走看看