帮助搜索多个下拉标签

时间:2012-10-08 作者:Mowgs Ly

我正在尝试构建一个搜索表单,允许用户选择特定的帖子标签并过滤房地产列表。到目前为止,我已经构建了表单,但当我单击搜索按钮时,它会将我带到网站的主页。然后,站点的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(\'&laquo; Previous\') ?></div>
                    <div class="nextLink"><?php next_posts_link(\'More &raquo;\') ?></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(); ?>

1 个回复
SO网友:miahelf

你的主题有搜索吗。php显示结果?如果没有,那么根据codex示例或默认主题创建一个,或者将源代码发布到索引中。php用于故障排除。

编辑:您的搜索。php很难调试,请尝试遵循默认主题的结构,并在search.php 要测试这是否是问题所在,请执行以下操作:

<?php get_header(); ?>
    <section id="primary" class="site-content">
        <div id="content" role="main">
        <?php if ( have_posts() ) : ?>
            <?php while ( have_posts() ) : the_post(); ?>
                <?php get_template_part( \'content\', get_post_format() ); ?>
            <?php endwhile; ?>
        <?php else : ?>
            <article>
                <div class="entry-content">
                    <p>Nothing found, do you want to search?</p>
                    <?php get_search_form(); ?>
                </div><!-- .entry-content -->
            </article><!-- #post-0 -->
        <?php endif; ?>
        </div><!-- #content -->
    </section><!-- #primary -->
<?php get_footer(); ?>

结束

相关推荐

display custom portfolio tags

这是代码的一部分,用于显示投资组合类别:$post_cat = array(); $post_cat = wp_get_object_terms($post->ID, \"portfolio_category\"); $post_cats = array(); $attachments = get_post_meta($post->ID, \'300-160-im