天天看点

2019.2.18 模板的创建和自定义子模板文件(single.php 等)

1、将主页面index.php分成不同的模板

  1. 创建header.php(头部)、sidebar.php(工具栏)、footer.php等模板
  2. 将对应的模块代码方法这些模板里面如header.php内容包括head声明的属性、头部导航栏(封装成头部模板,以供多个页面复用)代码:

    -`

<title><?php bloginfo('name'); ?><?php wp_title(); ?></title>

<meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />
<meta name="generator" content="WordPress <?php bloginfo('version'); ?>" /> <!-- leave this for stats please -->

<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" target="_blank" rel="external nofollow"  type="text/css" media="screen" />
<link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="<?php bloginfo('rss2_url'); ?>" target="_blank" rel="external nofollow"  />
<link rel="alternate" type="text/xml" title="RSS .92" href="<?php bloginfo('rss_url'); ?>" target="_blank" rel="external nofollow"  />
<link rel="alternate" type="application/atom+xml" title="Atom 0.3" href="<?php bloginfo('atom_url'); ?>" target="_blank" rel="external nofollow"  />
<link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" target="_blank" rel="external nofollow"  />

<?php wp_get_archives('type=monthly&format=link'); ?>
<?php //comments_popup_script(); // off by default ?>
<?php wp_head(); ?>
           
<h1><a href="<?php bloginfo('url'); ?>" target="_blank" rel="external nofollow" ><?php bloginfo('name'); ?></a></h1>
    <?php bloginfo('description'); ?>

</div>`
           
  • 在index引用只需使用WordPress的特用函数:get_header();(其他模块的引用也是使用:get_sidebar();、get_footer() ; )
  • 其他模块文件使用同样的函数即可应用该模块

在使用搜索框的时候,可以封装一个搜索框的模块(就像一个自定义的封装好的按钮、表格或者其他组件),使用PHP导入函数,将另外一个文件的PHP代码完全导入到本模块板即可使用:include(src)

2、窗体化侧边栏

在WordPress就封装好很多侧边栏的小工具,可以手动在侧边栏div中引用函数展示出来,也可以使用后台管理员窗口对小工具进行管理
  1. 在侧边栏内容的外面加入函数标记侧边栏的地方:
<?php if ( function_exists('dynamic_sidebar') && dynamic_sidebar() ) : else : ?>

<?php endif; ?>
           
  1. 在function.php中加入代码:
<?php
if ( function_exists('register_sidebar') )
    register_sidebar();
?>
           
  1. 返回后台管理的小工具页面,即可以通过窗口管理侧边工具栏

3、自定义子模板文件

创建

single.php 日志单页文件

page.php 页面文件

archvie.php 分类和日期存档页文件

search.php 搜索页面文件

comments.php 留言区域文件(包括留言列表和留言框)

404.php 404错误页面

并且通过相应URL的访问,会进入到不同页面

  1. 复制index.php页面到single.php 日志单页文件,当点击某个日志的URL就会进入single.php 页面内容,通过获取本页面的日志可以展示本页面的日志文件
  2. 创建404.php页面,当输入的URL,与WordPress的路由规则匹配不上,会跳转到该页面。