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

时间:2016-08-27 06:54:04

标签: php wordpress

我找到了一段代码,允许我有两个独立的循环,所以我可以在两个不同的类别中显示搜索结果,这很好,因为它们的风格也不同。

我想帮助修改以下代码,包括显示未找到的结果'每个循环的消息,并为第二个博客添加分页'仅限类别,顶级商店'应显示最多6个结果。

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

 

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

<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>
相关问题