elementui如何在input 框中搜索_WP主题开发16:wordpress主题trans,搜索页模板如何创建?...

在wordpress主题的动态模板中,搜索页模板并不是必须的。但是,为了对用户更加的友好,可以让用户在wordpress网站上能够通过搜索来查找自己的想要的文章内容,我们还是有必要给trans主题添加一个搜索页模板。一般情况下,wordpress主题动态模板的搜索页的整体布局和文章列表页模板是差不多的,所以,我们可以参照文章列表页来做。

第一步:创建一个search.php文件。

在wordpress主题trans目录下创建一个search.php文件,这是wordpress程序要求的默认搜索页模板的文件,名字必须是search。然后,把trans静态模板search.html文件中的代码全部复制到search.php文件中。

第二步:修改头部的代码。

在search.php文件中,删除从< !doctype>到< /header>标签之间的所有代码。然后,在同样的位置,通过wordpress函数来引入公共的头部模板文件:

< ?php get_header(); //引入头部模板 ?>

修改头部模板header.php文件中的搜索表单的代码,代码如下:

< form action="< ?php bloginfo("siteurl"); ?>" method="get">

< input type="search" name="s" placeholder="搜索...">

< input type="submit" value="搜索">

8601ad9435eda9b76ed67399cd3b89eb.png

注:第一个input输入框的name值必须是 s (如上图),这也是wordpress的硬性要求。否则,就搜索不到任何内容。

第三步:修改search.php的侧边栏代码。

在search.php文件中,删除< div class="c_right">标签所包含的所有侧边栏代码,然后,在这个位置上,引入sidebar.php侧边栏公共模板:

< ?php get_sidebar(); //引入侧边栏模板 ?>

第四步:修改搜索页模板的底部代码。

在搜索页模板中,删除从< footer>标签到模板代码结束的所有底部代码,然后,在当前的位置上,添加如下代码引入底部公共模板:

< ?php get_footer(); //引入底部模板 ?>

第五步:修改search.php的左侧主体部分的面包屑导航。

因为是搜索页面模板,所以,这个面包屑导航,我们要体现出搜索结果。所以,我们删除search.php模板中< div class="left_top">标签内部的所有代码,替换成如下的代码:

< ul>< li>< span class="dashicons-before dashicons-admin-generic">搜索< li>< a href="<?php bloginfo("siteurl"); ?>">< span class="dashicons-before dashicons-admin-home">首页 > 搜索结果< ul> 关键词“< ?php the_search_query(); ?>”共有 < ?php global $wp_query; echo $wp_query->found_posts; ?> 个搜索结果

上面代码解说:

the_search_query():输出要搜索的关键词;$wp_query:是wordpress提供的一个全局变量,它包含当前页面的所有查询数据;$wp_query->found_posts:搜索当前关键词的数量

搜索页面包屑导航效果如下图:

9b9b7722b94e5e2205b0a6eef43755b0.png

第六步:修改搜索页模板左侧的文章列表。

删除search.php搜索页模板中< div class="left_bottom">标签内部的所有代码,然后,替换成如下代码:

< ?php if ( have_posts() ): while ( have_posts() ) : the_post();?>< div class="list">< ul class="list_h">< a href="< ?php the_permalink(); ?>">< ?php the_title(); ?>< ul class="list_con">< ul class="list_con_left">< a href="< ?php the_permalink(); ?>"> < ?php if(has_post_thumbnail()) { //如果有特色图片,就调用特色图片the_post_thumbnail( 'thumbnail' ,array( 'alt' => trim(strip_tags( $post->post_title )), 'title' => trim(strip_tags( $post->post_title )))); }else { //否则调用文章第一张图片echo 'post_title.'" />';}?>< ul class="list_con_right">< li>< ?php echo mb_substr(strip_tags($post->post_content),0,120,'utf-8'); ?> ... < li>< span class="dashicons-before dashicons-admin-users">< ?php the_author(); ?> < span class="dashicons-before dashicons-calendar-alt">< ?php the_time("Y年m月d日"); ?> < span class="dashicons-before dashicons-visibility">< ?php echo get_post_meta($post->ID, 'views', true); ?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值