zoukankan      html  css  js  c++  java
  • wordpress_Function Reference_get template part

    在wordpress3.0以后的版本可以看到这样的代码
    [php]
    /* Run the loop for the archives page to output the posts.
     * If you want to overload this in a child theme then include a file
     * called loop-archive.php and that will be used instead.
     */
     get_template_part( 'loop', 'archive' );
    [/php]

    如果你不明白可以参考: http://codex.wordpress.org/Function_Reference/get_template_part
    下面是我笨拙的翻译
    <strong>描述:</strong>
    加载一个制定的模板到另一个模板里面(不同于包含header,sidebar,footer).
    使得一个主题使用子模板来实现代码段重用变得简单

    用于在模板中包含指定的模板文件,只需用指定参数slug和name就可以包含文件{slug}-{name}.php,最重要的功能是如果没有这个文件就包含没有{slug}的.php文件文件

    <strong>使用方法:</strong>
    [php]<?php get_template_part( $slug, $name ) ?>[/php]

    <strong>参数:</strong>
    $slug (必须) 通用的模板名

    $name (可选) 指定的模板名

    <strong>示例:</strong>

    使用 loop.php 在子主题里面

    假设主题文件夹wp-content/themes下父主题是twentyten子主题twentytenchild,那么下面的代码:

    [php]<?php get_template_part( 'loop', 'index' ); ?>[/php]

    php 的require()函数将按下面优先级包含文件

     1. wp-content/themes/twentytenchild/loop-index.php
     2. wp-content/themes/twentytenchild/loop.php
     3. wp-content/themes/twentyten/loop-index.php
     4. wp-content/themes/twentyten/loop.php

    导航
    使用通用的nav.php文件给主题添加导航条:
    [php]
    <?php get_template_part( 'nav' );           // Navigation bar (nav.php) ?>
    <?php get_template_part( 'nav', '2' );      // Navigation bar #2 (nav-2.php) ?>
    <?php get_template_part( 'nav', 'single' ); // Navigation bar to use in single pages (nav-single.php) ?>
    [/php]
    This entry was posted in php and tagged wordpress

    转载自:wordpress_Function Reference_get template part

  • 相关阅读:
    2015年蓝桥杯省赛A组c++第3题
    2015年蓝桥杯省赛A组c++第1题
    算法学竞赛常用头文件模板
    Android+Tomcat通过http获取本机服务器资源
    Ubuntu16.04LTS卸载软件的命令
    20个有趣的Linux命令
    Ubuntu16.04开机蓝屏问题解决
    Ubuntu启动时a start job is running for dev-disk-by延时解决
    Cookie与Session
    JS 闭包
  • 原文地址:https://www.cnblogs.com/heui/p/1993147.html
Copyright © 2011-2022 走看看