Cómo mostrar los resultados de búsqueda de Wordpress?


He pasado mucho tiempo averiguando por qué mi búsqueda no funciona en mi plantilla personalizada. Hasta ahora he sido capaz de averiguar cómo incluir searchform.archivo php en mi encabezado, búsqueda creada.archivo php que actualmente está vacío (por lo que en el momento en que busco algo me redirige a una página en blanco y creo que definitivamente necesito algo en la búsqueda.php para que funcione), estaba leyendo todo el códice de Wordpress, pero no pude encontrar una solución, solo la información útil que encontré fue este.

Http://codex.wordpress.org/Creating_a_Search_Page

¿Puede sugerir lo que hay que hacer para mostrar los resultados de una búsqueda? hay una consulta especial, función, etc? Realmente no puedo encontrarlo en ninguna parte.

Mi formulario de búsqueda.archivo php en caso de que lo necesite.

<form action="<?php echo home_url(); ?>" id="search-form" method="get">
    <input type="text" name="s" id="s" value="type your search" onblur="if(this.value=='')this.value='type your search'"
    onfocus="if(this.value=='type your search')this.value=''" />
    <input type="hidden" value="submit" />
</form>
Author: Ilja, 2013-02-11

4 answers

Básicamente, necesitas incluir el bucle de Wordpress en tu búsqueda.plantilla php para recorrer los resultados de búsqueda y mostrarlos como parte de la plantilla.

A continuación se muestra un ejemplo muy básico de La Plantilla de Búsqueda de Tema de WordPress y la Plantilla de Página en ThemeShaper.

<?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(); ?>
 17
Author: James Hebden,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-02-10 21:04:33

Necesitas incluir el bucle de Wordpress en tu búsqueda.php este es el ejemplo

Busca.archivo de plantilla php:

<?php get_header(); ?>
<?php
$s=get_search_query();
$args = array(
                's' =>$s
            );
    // The Query
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
        _e("<h2 style='font-weight:bold;color:#000'>Search Results for: ".get_query_var('s')."</h2>");
        while ( $the_query->have_posts() ) {
           $the_query->the_post();
                 ?>
                    <li>
                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                    </li>
                 <?php
        }
    }else{
?>
        <h2 style='font-weight:bold;color:#000'>Nothing Found</h2>
        <div class="alert alert-info">
          <p>Sorry, but nothing matched your search criteria. Please try again with some different keywords.</p>
        </div>
<?php } ?>

<?php get_sidebar(); ?>
<?php get_footer(); ?>
 23
Author: Said Erraoudy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-05-06 11:56:28

Estoy usando archivos searchform.php y search.php como ya se mencionó, pero aquí proporciono el código real.

Crear una Página de Búsqueda codex la página ayuda aquí y #Creating_a_Search_Page_Template muestra la consulta de búsqueda.

En mi caso paso los argumentos $search_query al WP_Query Class (que puede determinar si es consulta de búsqueda!). Luego corro El Bucle para mostrar la información post que quiero, que en mi caso es el the_permalink y the_title.

Cuadro de búsqueda formulario:

<form class="search" method="get" action="<?php echo home_url(); ?>" role="search">
  <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' ) ?>" />
  <button type="submit" role="button" class="btn btn-default right"/><span class="glyphicon glyphicon-search white"></span></button>
</form>

search.php archivo de plantilla:

<?php
    global $query_string;
    $query_args = explode("&", $query_string);
    $search_query = array();

    foreach($query_args as $key => $string) {
      $query_split = explode("=", $string);
      $search_query[$query_split[0]] = urldecode($query_split[1]);
    } // foreach

    $the_query = new WP_Query($search_query);
    if ( $the_query->have_posts() ) : 
    ?>
    <!-- the loop -->

    <ul>    
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <li>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        </li>   
    <?php endwhile; ?>
    </ul>
    <!-- end of the loop -->

    <?php wp_reset_postdata(); ?>

<?php else : ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
 10
Author: Brownrice,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-04-21 15:35:51

Compruebe si su plantilla en la carpeta theme contiene search.php y searchform.php o no.

 4
Author: blueray,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-03-26 18:39:33