【问题标题】:Filtering Search Results with Wordpress使用 Wordpress 过滤搜索结果
【发布时间】:2010-01-22 15:14:14
【问题描述】:

我正在尝试设置一个包含两列的 搜索结果 页面。第一列将显示除一个 ( Galleries ) 之外的所有类别的结果,第二列将仅显示 Galleries 类别。

query_posts() 只是重置我的结果。这是我到目前为止得到的。破碎:

        <?php 
            $s = get_query_var('s');
            query_posts('s=' . $s . '&cat=164'); 
        ?>

        <?php 
            // First Loop 
        ?>
        <div class="contentLeft">
            <ul class="postListSmall related">
                <?php while (have_posts()) : the_post(); ?>
                    [do stuff]
                <?php endwhile; ?>

        <?php 
            // Second Loop
        ?>
            <?php query_posts('cat=-164'); ?>
            <?php rewind_posts(); ?>
                <?php while (have_posts()) : the_post(); ?>
                    [do stuff]
                <?php endwhile; ?>

    <?php else : ?>
                [do stuff]
    <?php endif; ?>

怎么办?

【问题讨论】:

标签: php wordpress templates wordpress-theming


【解决方案1】:

我知道这是一篇旧帖子,但我遇到了类似的问题,我想分享一下:

  1. 您正在创建一个查询,然后调用第二个查询,但随后尝试回退该查询。这不是倒带功能的工作方式。看看Rewind Documentation。你还说:

query_posts() 只是重置我的结果。

那你为什么在新查询之后立即调用 rewind 函数?另外,如果您要重置结果,那么为什么它完全是一个不同的查询?这个:

        $s = get_query_var('s');
        query_posts('s=' . $s . '&cat=164'); 

和这个不一样:

        <?php query_posts('cat=-164'); ?>
        <?php rewind_posts(); ?>

为了获得不同类别的 2 列结果,我执行了以下操作:仅使用一个循环,不使用倒带,在循环中的 if 语句中使用 get_the_category,例如:

<?php 
$s = get_query_var('s');
query_posts('s=' . $s . '&cat=164'); 

while (have_posts()) : the_post();
    foreach(get_the_category() as $category){
        if($category->name == "category name"){
            //Concatenate to the left div
        } else {
            //concatenate to the right div
        } ?>

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2012-08-30
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    • 2013-02-02
    • 2012-07-05
    • 2017-05-17
    • 1970-01-01
    相关资源
    最近更新 更多