欢迎访问我们的网站

选择语言

  • English
  • 简体中文
  • 日本語
  • 한국어
  • Français
  • Deutsch
  • Español
  • Português
  • Italiano
  • Русский
  • العربية
  • Nederlands
  • Türkçe
  • Polski
  • Svenska
  • Dansk

WordPress系列

12搜索调用–新手小白,快速使用静态页面建立wordpress主题

22.09.2025 21

在调用之前我们需要建立一个搜索页面,在搜索页面中我们需要调用搜索框和搜索结果,
搜索页面放在主题根目录下面,文件名为search.php (保留的特殊文件名称)
比如在主题根目录下面创建searchform.php文件,

直接用search.php文件,

  1. 搜索调用:
    1.1默认的搜索函数
TEXT
1234567
<form role="search" method="get" id="searchform" class="searchform" action="<?php echo home_url( '/' ); ?>">
    <div>
        <label class="screen-reader-text" for="s"><?php _x('Search for:', 'label'); ?></label>
        <input type="text" value="" name="s" id="s" />//搜索值为空,name="s" id="s" 
        <input type="submit" id="searchsubmit" value="<?php esc_attr_e('Search'); ?>" />
    </div>
</form>

注意:

  1. 搜索框的id是s
  2. 搜索按钮的id是searchsubmit
  3. 搜索框的name是s
  4. 搜索按钮的name是submit
  5. 搜索框的value是空
  6. 搜索按钮的value是Search
  7. 搜索框的type是text
  8. 搜索按钮的type是submit

本次模版案例:搜索定义原代码:

TEXT
1234567891011121314
 <!-- 搜索面板 -->
    <div class="search-panel" id="search-panel">
        <div class="container">
            <div class="search-panel-content">
                <form class="search-form">
                    <input type="text" placeholder="请输入搜索关键词...">
                    <button type="submit"><i class="fas fa-search"></i></button>
                </form>
                <div class="search-close" id="search-close">
                    <i class="fas fa-times"></i>
                </div>
            </div>
        </div>
    </div>

更改后的代码:

TEXT
123456789101112131415
<!-- 搜索面板 -->
    <div class="search-panel" id="search-panel">
        <div class="container">
            <div class="search-panel-content">
                <form class="search-form" id="searchform" action="<?php echo home_url( '/' ); ?>">//增加了action,id="searchform"
                    
                    <input type="text" placeholder="请输入搜索关键词..." name="s" id="s" value="">//增加了value="" name="s" id="s" 
                    <button type="submit" value="<?php esc_attr_e('Search'); ?>"><i class="fas fa-search"></i></button>
                </form>
                <div class="search-close" id="search-close">
                    <i class="fas fa-times"></i>
                </div>
            </div>
        </div>
    </div>

相比较静态代码增加了

TEXT
12345
  1. id="searchform" action="<?php echo home_url( '/' ); ?>"//在form中增加了action,id="searchform"

    2. value="" name="s" id="s" //在搜索栏中增加了value="" name="s" id="s" 

    3. value="<?php esc_attr_e('Search'); ?>"//在搜索按钮中增加了value="<?php esc_attr_e('Search'); ?>"
  1. 搜索结果
    2.1 搜索结果页面的模板
TEXT
12345678910111213141516171819202122
<?php get_header(); ?>
<div id="container">
    <div id="content" role="main">
        <h1 class="page-title">Search Results for: <?php the_search_query(); ?></h1>
        <?php if ( have_posts() ) : ?>
        <div id="nav-above" class="navigation">
            <div class="nav-previous"><?php next_posts_link() ?></div>
            <div class="nav-next"><?php previous_posts_link() ?></div>
        </div>
        <?php while ( have_posts() ) : the_post(); ?>
            <?php get_template_part( 'content', get_post_format() ); ?>
        <?php endwhile; ?>
        <div id="nav-below" class="navigation">
            <div class="nav-previous"><?php next_posts_link() ?></div>
            <div class="nav-next"><?php previous_posts_link() ?></div>
        </div>
        <?php else : ?>
            <p><?php _e( 'Sorry, but nothing matched your search criteria.' ); ?></p>
        <?php endif; ?>
    </div>
</div>
<?php get_footer(); ?>

微信二维码
抖音二维码