我正在尝试构建一个搜索表单,允许用户选择特定的帖子标签并过滤房地产列表。到目前为止,我已经构建了表单,但当我单击搜索按钮时,它会将我带到网站的主页。然后,站点的url将更改为正常url,并在末尾添加此url,以显示正在使用的标准:/?位置=-1和;属性类型=46(&;sqft=-1和;床位=-1和;浴室=-1&;车库=-1
我猜我需要的是在PHP中编写某种post操作,确定要搜索的内容,并创建一个包含搜索结果的页面。我完全不知道该怎么做。我几乎没有数据库经验,也不知道如何用PHP连接WordPress变量。
我相信一定有一种更简单的方法可以做到这一点,我只是在四处搜索了几天之后还没有找到它。请在这里帮助我,并向我指出任何有用的资源或您可以做的任何事情。
下面是我如何构建表单的,改编自http://codex.wordpress.org/Function_Reference/wp_dropdown_categories:
编辑:此代码位于我的searchform中。php文件,我在网站的每个页面上都调用它
<form role="search" method="get" id="searchform" action="<?php echo home_url( \'/\' ); ?>">
<div>
<!--<label class="screen-reader-text" for="s">Search for:</label>
<input type="text" value="" name="s" id="s" /> -->
<?php //wp_dropdown_categories( \'taxonomy=post_tag\' ); ?>
<h3 id="searchlabel">Search Listings</h3>
<?php
//this one shows the locations
$args = array(
\'taxonomy\' => \'post_tag\',
\'show_option_all\' => \'\',
\'show_option_none\' => \'Any\',
\'orderby\' => \'name\',
\'order\' => \'ASC\',
\'show_count\' => 0,
\'hide_empty\' => 1,
\'child_of\' => 0,
\'exclude\' => \'-46,-33,-6,-7,-41,-12,-13,-40,-37,-38,-39,-8,-9,-10,-11,-42,-43,-44,-45\',
\'echo\' => 1,
\'selected\' => 0,
\'hierarchical\' => 0,
\'name\' => \'locations\',
\'id\' => \'\',
\'class\' => \'postform\',
\'depth\' => 0,
\'tab_index\' => 0,
\'hide_if_empty\' => false
);
//property type
$args2 = array(
\'taxonomy\' => \'post_tag\',
\'show_option_all\' => \'\',
\'show_option_none\' => \'Any\',
\'orderby\' => \'name\',
\'order\' => \'ASC\',
\'show_count\' => 0,
\'hide_empty\' => 1,
\'child_of\' => 0,
\'exclude\' => \'-5,-28,-27,-29,-30,-34,-35,-2,-12,-13,-40,-37,-38,-39,-8,-9,-10,-11,-42,-43,-44,-45\',
\'echo\' => 1,
\'selected\' => 0,
\'hierarchical\' => 0,
\'name\' => \'property-type\',
\'id\' => \'\',
\'class\' => \'postform\',
\'depth\' => 0,
\'tab_index\' => 1,
\'hide_if_empty\' => false
);
//sq ft
$args3 = array(
\'taxonomy\' => \'post_tag\',
\'show_option_all\' => \'\',
\'show_option_none\' => \'Any\',
\'orderby\' => \'name\',
\'order\' => \'ASC\',
\'show_count\' => 0,
\'hide_empty\' => 1,
\'child_of\' => 0,
\'exclude\' => \'-46,-33,-6,-7,-41,-5,-28,-27,-29,-30,-34,-35,-2,-12,-13,-40,-37,-38,-39,-8,-9,-10,-11\',
\'echo\' => 1,
\'selected\' => 0,
\'hierarchical\' => 0,
\'name\' => \'sqft\',
\'id\' => \'\',
\'class\' => \'postform\',
\'depth\' => 0,
\'tab_index\' => 2,
\'hide_if_empty\' => false
);
//beds
$args4 = array(
\'taxonomy\' => \'post_tag\',
\'show_option_all\' => \'\',
\'show_option_none\' => \'Any\',
\'orderby\' => \'name\',
\'order\' => \'ASC\',
\'show_count\' => 0,
\'hide_empty\' => 1,
\'child_of\' => 0,
\'exclude\' => \'-46,-33,-6,-7,-41,-5,-28,-27,-29,-30,-34,-35,-2,-12,-13,-40,-37,-38,-39,-42,-43,-44,-45\',
\'echo\' => 1,
\'selected\' => 0,
\'hierarchical\' => 0,
\'name\' => \'beds\',
\'id\' => \'\',
\'class\' => \'postform\',
\'depth\' => 0,
\'tab_index\' => 3,
\'hide_if_empty\' => false
);
//baths
$args5 = array(
\'taxonomy\' => \'post_tag\',
\'show_option_all\' => \'\',
\'show_option_none\' => \'Any\',
\'orderby\' => \'name\',
\'order\' => \'ASC\',
\'show_count\' => 0,
\'hide_empty\' => 1,
\'child_of\' => 0,
\'exclude\' => \'-46,-33,-6,-7,-41,-5,-28,-27,-29,-30,-34,-35,-2,-37,-38,-39,-8,-9,-10,-11,-42,-43,-44,-45\',
\'echo\' => 1,
\'selected\' => 0,
\'hierarchical\' => 0,
\'name\' => \'baths\',
\'id\' => \'\',
\'class\' => \'postform\',
\'depth\' => 0,
\'tab_index\' => 4,
\'hide_if_empty\' => false
);
//garage size
$args6 = array(
\'taxonomy\' => \'post_tag\',
\'show_option_all\' => \'\',
\'show_option_none\' => \'Any\',
\'orderby\' => \'name\',
\'order\' => \'ASC\',
\'show_count\' => 0,
\'hide_empty\' => 1,
\'child_of\' => 0,
\'exclude\' => \'-46,-33,-6,-7,-41,-5,-28,-27,-29,-30,-34,-35,-2,-12,-13,-40,-8,-9,-10,-11,-42,-43,-44,-45\',
\'echo\' => 1,
\'selected\' => 0,
\'hierarchical\' => 0,
\'name\' => \'garage\',
\'id\' => \'\',
\'class\' => \'postform\',
\'depth\' => 0,
\'tab_index\' => 5,
\'hide_if_empty\' => false
);
wp_dropdown_categories($args);
wp_dropdown_categories($args2);
wp_dropdown_categories($args3);
wp_dropdown_categories($args4);
wp_dropdown_categories($args5);
wp_dropdown_categories($args6);
?>
<input type="submit" id="searchsubmit" value="Search" />
</div>
</form>
搜索代码。php:
<?php
/*
Template Name: Search Results Page
*/
get_header(); ?>
<div id="contentWrap">
<div id="wholeSidebar">
<?php get_sidebar(); ?>
<img src="../../../../img/ehomls.jpg" width="185" height="72" alt="" id="realtorImg"/>
</div>
<div id="content">
<?php if ( have_posts() ) : ?>
<header class="page-header">
<h1 class="page-title"><?php printf( __( \'Search Results for: %s\', \'custom2\' ), \'<span>\' . get_search_query() . \'</span>\' ); ?></h1>
</header>
<?php //WHAT IS THIS?--> twentyeleven_content_nav( \'nav-above\' ); ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
/* Include the Post-Format-specific template for the content.
* If you want to overload this in a child theme then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
/* -MY CODE-
<?php if (have_posts()) : while(have_posts()) : $i++; if(($i % 2) == 0) : $wp_query->next_post(); else : the_post(); ?>
<div class="left-column">
<?php the_content(); ?>
</div>
<?php endif; endwhile; else:endif; ?>
<?php $i = 0; rewind_posts(); ?>
<?php if (have_posts()) : while(have_posts()) : $i++; if(($i % 2) !== 0) : $wp_query->next_post(); else : the_post(); ?>
<div class="right-column">
<?php the_content(); ?>
</div>
<?php endif; endwhile; else:endif; ?>
<!--<div class="pagination">-->
<div class="prevLink"><?php previous_posts_link(\'« Previous\') ?></div>
<div class="nextLink"><?php next_posts_link(\'More »\') ?></div>*/
//DEFAULT CODE
get_template_part( \'content\', get_post_format() );
?>
<?php endwhile; ?>
<?php //WHAT IS THIS?--> twentyeleven_content_nav( \'nav-below\' ); ?>
<?php else : ?>
<article id="post-0" class="post no-results not-found">
<header class="entry-header">
<h1 class="entry-title"><?php _e( \'Nothing Found\', \'custom2\' ); ?></h1>
</header><!-- .entry-header -->
<div class="entry-content">
<p><?php _e( \'Sorry, but nothing matched your search criteria. Please try again with some different keywords.\', \'custom2\' ); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
</article><!-- #post-0 -->
<?php endif; ?>
</div><!-- #content -->
<?php get_footer(); ?>