doujiyong7604 2016-08-27 06:54
浏览 65
已采纳

Wordpress搜索按类别生成多个循环

I found a piece of code that allows me to have two separate loops so I can display search results in two different categories, which is great because they are also styled different.

I would like help amending this code below to include showing a 'no results found' message for each loop and add pagination for the second 'blog' category only, the top one 'shop' should show a max of 6 results.

<?php
get_header(); ?>
<div class="content">
        <?php
            $s = get_search_query();
        ?>
<div class="search">
    <div class="categoryThumbs">
    <?php if (have_posts()) : ?>
        <h3><?php printf( __( 'Search Results for: %s'), '<span>' . get_search_query() . '</span>' ); ?></h3>
    <?php endif;?>
        <?php query_posts("s='$s'&category_name=shop"); ?>
            <?php if (have_posts()) : ?>
                <?php $blogResults=0; ?>
            <?php while (have_posts()) : the_post(); ?>
                <?php
                    $blogResults++;
                ?>
            <?php endwhile; ?>
                <h4><?php echo $blogResults; ?> Results in Shop</h4>
                <?php while (have_posts()) : the_post(); ?>
                <div class="films">
                    <div class="thumb">
                        <a href="<?php the_permalink() ?>"><?php the_post_thumbnail(); ?></a>
                    </div>
                    <h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
                    <div class="entry">
                        <?php the_excerpt() ?>
                    </div>
                </div>
                <?php endwhile; ?>
            <?php endif;?>
            <?php query_posts("s='$s'&category_name=blog"); ?>
            <?php if (have_posts()) : ?>
                <?php $blogResults=0; ?>
            <?php while (have_posts()) : the_post(); ?>
                <?php
                    $blogResults++;
                ?>
            <?php endwhile; ?>
                <h4><?php echo $blogResults; ?> Results in Blog</h4>
                <?php while (have_posts()) : the_post(); ?>
                <div class="films">
                    <div class="thumb">
                        <a href="<?php the_permalink() ?>"><?php the_post_thumbnail(); ?></a>
                    </div>
                    <h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
                    <div class="entry">
                        <?php the_excerpt() ?>
                    </div>
                </div>
                <?php endwhile; ?>
            <?php endif;?>
    <div class="spacer"></div>
    </div>
</div>

Thanks for any help!

  • 写回答

1条回答 默认 最新

  • drzb7969753 2016-08-27 07:14
    关注
    <div class="search">
    <div class="categoryThumbs">
    <?php if (have_posts()) : ?>
        <h3><?php printf( __( 'Search Results for: %s'), '<span>' . get_search_query() . '</span>' ); ?></h3>
    <?php endif;?>
            <?php if (have_posts()) : ?>
                <h4><?php echo wp_count_posts(); ?> Results in Shop</h4>
                <?php while (have_posts()) : the_post(); 
                  $taxonomy = 'category';
                  $queried_term = get_query_var($taxonomy);
                  $term = get_term_by( 'slug', $queried_term, $taxonomy );
                  $termName =  $term->name;
                  if($termName == 'shop'):
                 ?>
                <div class="films">
                    <div class="thumb">
                        <a href="<?php the_permalink() ?>"><?php the_post_thumbnail(); ?></a>
                    </div>
                    <h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
                    <div class="entry">
                        <?php the_excerpt() ?>
                    </div>
                </div>
                <?php else: 
                  // others code for blog
                 endif; endwhile; ?>
            <?php endif;?>
    
    <div class="spacer"></div>
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试