dongweng9474 2010-01-22 15:14
浏览 34

使用Wordpress过滤搜索结果

I'm trying to setup a Search Results page with two columns. First column will present results from all categories except one ( Galleries ), and the second column will present only the Galleries category.

query_posts() simply resets my results. This is what I got so far. Broken:

        <?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; ?>

What to do?

  • 写回答

1条回答 默认 最新

  • duanshaiduhao2471 2017-03-13 18:17
    关注

    I know this is an old post but I am having a similar problem and thought I would share:

    1. You are creating a query, then calling a second query, but then trying to rewind the query. That's not how the rewind function works. Take a look at the Rewind Documentation. You also say:

    query_posts() simply resets my results.

    Then why are you calling the rewind function immediately after the new query? Also, if you're resetting the results then why is it a different query completely? This:

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

    Is not the same as this:

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

    To get 2 column results for different categories I did the following: use only one loop, don't use rewind, use get_the_category in an if statement in your loop, for example:

    <?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
            } ?>
    

    Hope this helps.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。