以WordPress主题制作搜索表单和搜索页面

时间:2018-01-02 作者:maverick

如何在wordpress主题中创建搜索表单并将其链接到搜索。将在该页面中显示结果的php页面?我有一个如下的搜索表单:

<form role="search" method="get" class="search-form" action="<?php echo esc_url( home_url( \'/\' ) ); ?>">
                                    <div class="input-group search-block">
                                        <span class="screen-reader-text">Search for:</span>
                                        <input type="search" class="search-field form-control" placeholder="<?php esc_attr_e( \'Search &hellip;\', \'shape\' ); ?>" value="<?php echo esc_attr( get_search_query() ); ?>" name="s" title="Search for:" />
                                        <div class="input-group-btn">
                                            <input type="submit" class="search-submit btn btn-default" value="<?php esc_attr_e( \'Search\', \'shape\' ); ?>" />
                                        </div>
                                    </div>
                                </form>
和搜索。看起来像这样的php

<?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

                    global $query_string;

                    $search_query = wp_parse_str( $query_string );
                    $search = new WP_Query( $search_query );
                    global $wp_query;
                    $total_results = $wp_query->found_posts;
                ?>
            <?php endif; ?>

            </div><!-- #content .site-content -->
        </section><!-- #primary .content-area -->

<?php get_footer(); ?>
问题是除了键入的字符外,它不会显示任何内容。

1 个回复
SO网友:Mostafa Norzade

默认HTML5表单。添加主题支持

function wpdocs_after_setup_theme() {
    add_theme_support( \'html5\', array( \'search-form\' ) );
}
add_action( \'after_setup_theme\', \'wpdocs_after_setup_theme\' );
WordPress将呈现其内置的HTML5搜索表单:

<form role="search" method="get" class="search-form" action="<?php echo 
home_url( \'/\' ); ?>">
    <label>
        <span class="screen-reader-text"><?php echo _x( \'Search for:\', \'label\' ) ?></span>
        <input type="search" class="search-field"
            placeholder="<?php echo esc_attr_x( \'Search …\', \'placeholder\' ) ?>"
            value="<?php echo get_search_query() ?>" name="s"
            title="<?php echo esc_attr_x( \'Search for:\', \'label\' ) ?>" />
    </label>
    <input type="submit" class="search-submit"
        value="<?php echo esc_attr_x( \'Search\', \'submit button\' ) ?>" />
</form>
在搜索中插入以下代码。php

<div class="wrap">

<header class="page-header">
    <?php if ( have_posts() ) : ?>
        <h1 class="page-title"><?php printf( __( \'Search Results for: %s\', \'twentyseventeen\' ), \'<span>\' . get_search_query() . \'</span>\' ); ?></h1>
    <?php else : ?>
        <h1 class="page-title"><?php _e( \'Nothing Found\', \'twentyseventeen\' ); ?></h1>
    <?php endif; ?>
</header><!-- .page-header -->

<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">

    <?php
    if ( have_posts() ) :
        /* Start the Loop */
        while ( have_posts() ) : the_post();
            /**
             * Run the loop for the search to output the results.
             * If you want to overload this in a child theme then include a file
             * called content-search.php and that will be used instead.
             */
            get_template_part( \'components/post/content\', \'excerpt\' );
        endwhile; // End of the loop.
        the_posts_pagination( array(
            \'prev_text\' => twentyseventeen_get_svg( array( \'icon\' => \'previous\' ) ) . \'<span class="screen-reader-text">\' . __( \'Previous page\', \'twentyseventeen\' ) . \'</span>\',
            \'next_text\' => \'<span class="screen-reader-text">\' . __( \'Next page\', \'twentyseventeen\' ) . \'</span>\' . twentyseventeen_get_svg( array( \'icon\' => \'next\' ) ),
            \'before_page_number\' => \'<span class="meta-nav screen-reader-text">\' . __( \'Page\', \'twentyseventeen\' ) . \' </span>\',
        ) );
    else : ?>

        <p><?php _e( \'Sorry, but nothing matched your search terms. Please try again with some different keywords.\', \'twentyseventeen\' ); ?></p>
        <?php
            get_search_form();
    endif;
    ?>

    </main><!-- #main -->
</div><!-- #primary -->
<?php get_sidebar(); ?>

结束

相关推荐

is_search called incorreclty

我刚刚在我的一个网站页面上发现了一条新的错误消息,上面列出了帖子,http://clubcorvettect.com/category/events/ :注意:is\\U搜索调用不正确。运行查询之前,条件查询标记不起作用。在此之前,它们总是返回false。有关详细信息,请参阅WordPress中的调试。(此消息是在版本3.1.0中添加的。)in/home/wp\\U gmz28r/clubcorvettect。com/wp包括/功能。php在线4138这个网站已经正常运行了几个月了。我尝试添加这个论坛中建