The Search Template and The Page Template are vital to any complete WordPress Theme. And they’re both really easy to code.
For both of these Templates, we’ll start with our template-template,single.php, again. As a refresher, here’s single.php.

<?php
/**
* The Template for displaying all single posts.
*
* @package Shape
* @since Shape 1.0
*/
get_header ();
?>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php shape_content_nav( 'nav-above' ); ?>
<?php get_template_part( 'content', 'single' ); ?>
<?php shape_content_nav( 'nav-below' ); ?>
<?php
// If comments are open or we have at least one comment, load up the comment
// template
if (comments_open () || '0' != get_comments_number ())
comments_template ( '', true );
?>
<?php endwhile; // end of the loop. ?>
</div>
<!-- #content .site-content -->
</div>
<!-- #primary .content-area -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>

But, of course, each one is going to take its own path.

The Search Template

In search.php we’re going to wrap our loop inside an IF statement—in case we don’t have any posts to loop through.

Here’s how it’ll work: IF we have posts, or, in other words, if there are posts that match the terms we’re searching for, THEN loop through them, sorta just like in index.php, but IF we don’t have posts to loop through, or, if there aren’t any posts that match our search terms, give our searchers another chance at this searching business.

In code, it would look like this:

<?php
/**
* The template for displaying Search Results pages.
*
* @package Shape
* @since Shape 1.0
*/
get_header(); ?>
<section id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php if ( have_posts() ) : ?>
<header class="page-header">
<h1 class="page-title">
<?php printf( __( 'Search Results for: %s', 'shape' ), '<span>' . get_search_query() . '</span>' ); ?></h1>
</header><!-- .page-header -->
<?php shape_content_nav( 'nav-above' ); ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'search' ); ?>
<?php endwhile; ?>
<?php shape_content_nav( 'nav-below' ); ?>
<?php else : ?>
<?php get_template_part( 'no-results', 'search' ); ?>
<?php endif; ?>
</div><!-- #content .site-content -->
</section><!-- #primary .content-area -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>

Pretty straightforward, right? Let’s cover a few areas in the code.

First, take a look at everything between if ( have_posts() ) :(line 14) and shape_content_nav( 'nav-below' );. If there are search results, this is the markup that controls how the search results page will look. We’ll get a nice title at the top that includes the search term, followed by a listing of the posts returned in the search.

Also, shape_content_nav() works the same way here as it does inindex.php.

You’ll remember get_template_part() from earlier lessons. It’s loading the file containing the actual markup for the posts returned in the search. It’ll first look for a file called “content-search.php”, but since we are not creating that file, it’ll use content.php instead. If you wanted to format your search results posts differently (just like we did for Aside posts), feel free to create a “content-search.php” file and go wild!

Next, let’s look at the code between else : (line 31) and endif; (line 35). If there are no search results to display, we want to let our visitor know, nicely. To do that, we load the same no-results.php file that we created in the index.php lesson. It sure feels great when we can resuse code without needing to actually paste it into each file.

The Search Form Template
Back in WordPress Theme Template & Directory Structure, we addedsearchform.php. If you want to customize the markup of your theme’s search form, this is the file you’d use to make that happen. Otherwise, searchform.php is optional. If your theme doesn’t have asearchform.php, then WordPress displays the search form with its own built-in markup.

Here’s the code to paste into searchform.php.

<?php
/**
* The template for displaying search forms in Shape
*
* @package Shape
* @since Shape 1.0
*/
?>
<form method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>" role="search">
<label for="s" class="assistive-text"><?php _e( 'Search', 'shape' ); ?></label>
<input type="text" class="field" name="s" value="<?php echo esc_attr( get_search_query() ); ?>"
id="s" placeholder="<?php esc_attr_e( 'Search &hellip;', 'shape' ); ?>" />
<input type="submit" class="submit" name="submit" id="searchsubmit" value="<?php esc_attr_e( 'Search', 'shape' ); ?>" />
</form>

The Page Template

You know what the Page Template is for. WordPress thinks of it as a post out of chronological order. We think of it as, well a page. But largely, it’s defined by what it doesn’t have: all of the usual metadata (such as navigation, timestamps, categories, and tags) included a blog post.

Our Page template file will look very much like single.php. We will also place the contents of the Page loop inside content-page.php.

First, let’s set up the Page template. Open page.php and add the following code.

<?php
/**
* The template for displaying all pages.
*
* This is the template that displays all pages by default.
* Please note that this is the WordPress construct of pages
* and that other 'pages' on your WordPress site will use a
* different template.
*
* @package Shape
* @since Shape 1.0
*/
get_header(); ?>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
</div><!-- #content .site-content -->
</div><!-- #primary .content-area -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>

Now, let’s place the markup for the actual Page inside content-page.php. Paste the following into content-page.php.

<?php
/**
* The template used for displaying page content in page.php
*
* @package Shape
* @since Shape 1.0
*/
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
</header><!-- .entry-header -->
<div class="entry-content">
<?php the_content(); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-links">' . __( 'Pages:', 'shape' ), 'after' => '</div>' ) ); ?>
<?php edit_post_link( __( 'Edit', 'shape' ), '<span class="edit-link">', '</span>' ); ?>
</div><!-- .entry-content -->
</article><!-- #post-<?php the_ID(); ?> -->

It’s a stripped-down version of content-single.php. We’ve omitted all of the metadata that’s associated with posts.

And with that, you’re done with this lesson. Enjoy your new Page and Search templates.

WordPress 主题开发 - (十二) Search模板与Page模板 待翻译的更多相关文章

  1. 黄聪:《跟黄聪学WordPress主题开发》

    又一个作品完成!<跟黄聪学Wordpress主题开发>,国内最好的Wordpress主题模版开发视频教程!! 目录预览: WordPress官方源文件层式结构讲解 WordPress数据库 ...

  2. wordpress 主题开发

    https://yusi123.com/3205.html https://themeshaper.com/2012/10/22/the-themeshaper-wordpress-theme-tut ...

  3. 专题开发十二:JEECG微云高速开发平台-基础用户权限

      专题开发十二:JEECG微云高速开发平台-基础用户权限 11.3.4自己定义button权限 Jeecg中.眼下button权限设置,是通过对平台自己封装的button标签(<t:dgFun ...

  4. 敏捷宣言(Agile Manifesto)和敏捷开发十二原则

    敏捷宣言 The Agile Manifesto Individuals and interactions over Process and tools 个体与交互 重于 过程和工具 Working ...

  5. STC8H开发(十二): I2C驱动AT24C08,AT24C32系列EEPROM存储

    目录 STC8H开发(一): 在Keil5中配置和使用FwLib_STC8封装库(图文详解) STC8H开发(二): 在Linux VSCode中配置和使用FwLib_STC8封装库(图文详解) ST ...

  6. WordPress 主题开发 - (八) Head模板 待翻译

    THE WORDPRESS THEME HEADER TEMPLATE Now we get into the nitty-gritty: building up your header.php an ...

  7. WordPress 主题开发 - (二) 理解主题 待翻译

    What is “Theme Sense”? What is “Theme Sense”? Theme Sense is an intuitive understanding of WordPress ...

  8. [转]WordPress主题开发:主题初始化

    本文转自:http://www.cnblogs.com/tinyphp/p/4391182.html 在最简单的情况下,一个WordPress主题由两个文件构成: index.php -------- ...

  9. WordPress主题开发:主题初始化

    在最简单的情况下,一个WordPress主题由两个文件构成: index.php ------------------主模版 style.css  -------------------主样式表(注意 ...

随机推荐

  1. Eclipse Android HH and theme

    一. 汉化方法: 1.Eclipse版本查询:安装目录readme,查版本号;参照查代号如下表: 代号 平台版本 项目 主要版本发行日期 SR1发行日期 SR2发行日期 N/A 3.0 [1] N/A ...

  2. 蓝桥杯---波动数列(dp)(背包)(待解决)

    问题描述 观察这个数列: 1 3 0 2 -1 1 -2 ... 这个数列中后一项总是比前一项增加2或者减少3. 栋栋对这种数列很好奇,他想知道长度为 n 和为 s 而且后一项总是比前一项增加a或者减 ...

  3. LeetCode:237

    题目:Delete Node in a Linked List(从列表中删除指定结点) 描述:Write a function to delete a node (except the tail) i ...

  4. 【Unity Shaders】学习笔记——SurfaceShader(七)法线贴图

    [Unity Shaders]学习笔记——SurfaceShader(七)法线贴图 转载请注明出处:http://www.cnblogs.com/-867259206/p/5627565.html 写 ...

  5. windows7 下的日期没有internet时间的选项卡

    原因1:你在某个域里面,退出就可以了 原因2:你把windows Time的服务给禁掉或者关掉了, 步骤:运行,输入services.msc 确定,查找到windows Time开了即可

  6. php添加环境变量

    在php的安装目录中添加,如/usr/php-5.6.16添加env.php文件,在文件中设置环境变量: 如:<?php$_SERVER['ENV'] = 'production'; 再到配置文 ...

  7. FindResource函数错误代码:1813-找不到映像文件中指定的资源类型 与LoadResource函数错误代码:1812-指定的映像文件不包含资源区域

    HRSRC WINAPI FindResource( _In_opt_  HMODULE hModule, _In_      LPCTSTR lpName, _In_      LPCTSTR lp ...

  8. 如何在MAC机器中实现移动设备WiFI上网(没有专门的无线路由器的情况)

    在很多办公室甚至家中都有无线路由器以方便通过WIFI信号上网.如果没有无线路由器,如何让多个移动智能终端同时上网呢?如果你是Windows用户那么可以选择wifi共享精灵来解决,如果你拥有MAC机器, ...

  9. 【MySQL】触发器学习

    MySQL手册中对触发器的定义是: 触发程序是与表有关的命名数据库对象,当表上出现特定事件时,将激活该对象.表必须是永久性表,不能将触发程序与临时表与视图关联起来. 相同触发程序动作时间和事件的给定表 ...

  10. Flexbox布局详解

    弹性框布局 (flexbox) 添加了级联样式表级别 2 修订版 1 (CSS2.1) 中定义的四个基本布局模式:块布局.内联布局.表格布局和定位布局.使用弹性框布局功能,你可以更加轻松地设计复杂网页 ...