【问题标题】:Search Form not working on Wordpress搜索表单在 Wordpress 上不起作用
【发布时间】:2014-10-01 21:25:13
【问题描述】:

我在 wordpress 上的搜索表单有问题。当我尝试搜索任何术语时,它会给我“抱歉,没有帖子匹配您的查询”响应(我也安装了“搜索一切”插件)。我正在使用儿童主题。所以我尝试更改子 search.php 中的代码(将其复制给子),并在子中创建了一个 searchform.php。这是我目前正在使用的两个代码:

搜索.PHP:

> <?php
/**
 * Search Template
 *
 * The search template is used to display search results from the native WordPress search.
 *
 * If no search results are found, the user is assisted in refining their search query in
 * an attempt to produce an appropriate search results set for the user's search query.
 *
 * @package WooFramework
 * @subpackage Template
 */

 get_header();
 global $woo_options;
?>      
    <!-- #content Starts -->
    <?php woo_content_before(); ?>
    <div id="content" class="col-full">

        <div id="main-sidebar-container">    

            <!-- #main Starts -->
            <?php woo_main_before(); ?>
            <section id="main" class="col-left">

                <header class="page-header">
                <h1 class="page-title"><?php printf( __( 'Search Results for: %s', '' ), get_search_query() ); ?></h1>
            </header><!-- .page-header -->
<?php
$query = new WP_Query( array( 's' => get_query_var('s')) );

// The Loop
if ( $query->have_posts() ) {
    echo '<ul>';
    while ( $query->have_posts() ) {
        $query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }
    echo '</ul>';
} 
?>
<?php var_dump($the_query) ?>
            </section><!-- /#main -->

            <?php get_sidebar(); ?>

        </div><!-- /#main-sidebar-container -->         

        <?php get_sidebar( 'alt' ); ?>       

    </div><!-- /#content -->
    <?php woo_content_after(); ?>
<?php get_footer(); ?>

SEARCHFORM.PHP(我从二十二个主题中得到):

<form method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
        <label for="s" class="assistive-text"><?php _e( 'Search', 'twentyeleven' ); ?></label>
        <input type="text" class="field" name="s" id="s" placeholder="<?php esc_attr_e( 'Search', 'twentyeleven' ); ?>" />
        <input type="submit" class="submit" name="submit" id="searchsubmit" value="<?php esc_attr_e( 'Search', 'twentyeleven' ); ?>" />
    </form>

我也为 searchform.php 尝试了不同的代码,例如:

  <form method="get" id="searchform" action="<?php bloginfo('url'); ?>/">
        <label class="hidden" for="s"><?php _e('Search:'); ?></label>
        <input type="text" value="<?php the_search_query(); ?>" name="s" id="s" />
        <input type="submit" id="searchsubmit" value="GO" />
    </form>

<form role="search" method="get" id="searchform" class="searchform" action="<?php esc_url( home_url( '/' )); ?>">
    <div>
        <label class="screen-reader-text" for="s"><?php _x( 'Search for:', 'label' ); ?></label>
        <input type="text" value="<?php get_search_query(); ?>" name="s" id="s" />
        <input type="submit" id="searchsubmit" value="<?php esc_attr_x( 'Search', 'submit button' ); ?>" />
    </div>
</form>

<form role="search" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>">
    <label>
        <span class="screen-reader-text">Search for:</span>
        <input type="search" class="search-field" placeholder="Search …" value="" name="s" title="Search for:" />
    </label>
    <input type="submit" class="search-submit" value="Search" />
</form>

但还是一无所获。当它搜索时,这就是我在 url 中得到的:

http://www.energyhealing.directory/?s=energy&submit=Search

并在该 URL 的页面上显示:

搜索结果:能源

列入 继续阅读

如果有人可以向我解释我哪里出错了以及为什么出错,那会很棒吗?

提前感谢您提供的任何帮助。期待您的回音!

好的,我现在知道如何在问题中发帖了:

这是 search.php 中的代码,它只返回空白页:

  <?php
/**
 * Search Template
 *
 * The search template is used to display search results from the native WordPress search.
 *
 * If no search results are found, the user is assisted in refining their search query in
 * an attempt to produce an appropriate search results set for the user's search query.
 *
 * @package WooFramework
 * @subpackage Template
 */

 get_header();
 global $woo_options;
?>      
    <!-- #content Starts -->
    <?php woo_content_before(); ?>
    <div id="content" class="col-full">

        <div id="main-sidebar-container">    

            <!-- #main Starts -->
            <?php woo_main_before(); ?>
            <section id="main" class="col-left">

                <header class="page-header">
                <h1 class="page-title"><?php printf( __( 'Search Results for: %s', '' ), get_search_query() ); ?></h1>
            </header><!-- .page-header -->
<?php
$query = new WP_Query( array( 's' => get_query_var('s')) );

// The Loop
if ( $query->have_posts() ) {
    echo '<ul>';
    while ( $query->have_posts() ) {
        $query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }
    echo '</ul>';
} 
?>
            </section><!-- /#main -->

            <?php get_sidebar(); ?>

        </div><!-- /#main-sidebar-container -->         

        <?php get_sidebar( 'alt' ); ?>       

    </div><!-- /#content -->
    <?php woo_content_after(); ?>
<?php get_footer(); ?>

【问题讨论】:

  • 您的 search.php 中的代码似乎不完整。

标签: wordpress search


【解决方案1】:

已编辑(用于 search.php):

<?php if ( have_posts() ):?> 
        <ul>
        <?php while ( have_posts() ) : the_post();?>

            <li><?php the_title();?></li>
        <?php endwhile;?>
        </ul>
 <?php endif;?>

我认为 searchform.php 没问题,但我在 search.php 的任何地方都看不到循环

$query = new WP_Query( array( 's' => get_query_var('s')) );


// The Loop
if ( $query->have_posts() ) {
    echo '<ul>';
    while ( $query->have_posts() ) {
        $query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }
    echo '</ul>';
} 

您需要此查询来创建基于搜索的循环。

【讨论】:

  • 嗨,汤姆,感谢您的评论!我尝试添加您提供的代码,但现在它只打印: $query = new WP_Query(array('s' => get_query_var('s'))); // 循环 if ( $query->have_posts() ) { echo '
      '; while ( $query->have_posts() ) { $query->the_post();回声'
    • '。 get_the_title() 。 '
    • '; } 回声'
    '; }
  • 对不起,当我搜索一个词时,会在搜索表单区域打印?再次感谢!
  • 您需要将其包装在 PHP 标记中。
  • 可以放入所有代码,但我试过了: get_query_var('s'))); // 循环 if ( $query->have_posts() ) { echo '
      '; while ( $query->have_posts() ) { $query->the_post();回声'
    • '。 get_the_title() 。 '
    • '; } 回声'
    '; } ?>
  • 但是当我搜索某些内容和“搜索结果:能量”时,我现在只是得到一个空白屏幕。虽然下面是空白的
猜你喜欢
  • 2017-06-12
  • 1970-01-01
  • 1970-01-01
  • 2019-02-04
  • 1970-01-01
  • 2016-03-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多