douba5540 2011-11-16 20:15
浏览 45
已采纳

Wordpress搜索

is there a way to just search

  • pages
  • Custom Types
  • Posts

I am currently modifying the twenty eleven theme.

here is the Code for search.php:

<?php if ( have_posts() ) : ?>

            <header class="page-header">
                <h1 class="page-title"><?php printf( __( 'Search Results for "%s"', 'twentyeleven' ), '<span>' . get_search_query() . '</span>' ); ?></h1>
                <div class="SearchCount"><?php /* Search Count */ 
                $allsearch = &new WP_Query("s=$s&showposts=-1"); 
                //$key = wp_specialchars($s, 1); 
                $count = $allsearch->post_count; _e(''); _e(); 
                    echo $key; _e(); _e(); 
                    echo $count . ' '; _e(''); 
                wp_reset_query(); ?> Saved Results </div>
                <div id="topPagination"><?php twentyeleven_child_content_nav( 'nav-above' ); ?></div>
            </header>

            <?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.
                     */
                    get_template_part( 'content', 'search' );
                ?>

            <?php endwhile; ?>

            <?php twentyeleven_child_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', 'twentyeleven' ); ?></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.', 'twentyeleven' ); ?></p>
                    <?php get_search_form(); ?>
                </div><!-- .entry-content -->
            </article><!-- #post-0 -->

        <?php endif; ?>

And here is the content-search.php

<h2><a href="<?php the_permalink();?>"> 
        <?php $title = get_the_title(); $keys= explode(" ",$s); $title = preg_replace('/('.implode('|', $keys) .')/iu', '<strong class="search-excerpt">\0</strong>', $title); ?>
        <?php echo $title; ?>
    </a><br />
<span class="categoryClass"><?php the_category(','); ?></span></h2>

    <?php the_excerpt(); ?>
    <?php wp_link_pages( array( 'before' => '<div class="page-link"><span>' . __( 'Pages:', 'twentyeleven' ) . '</span>', 'after' => '</div>' ) ); ?>
</div><!-- .entry-content -->

Thank you

  • 写回答

2条回答 默认 最新

  • dream8877 2011-11-16 20:40
    关注

    The way I see it you have two options. One is to modify wp_query to only return the type that you want. The second is search on everything, but filter the results afterwards.

    This is how I believe you would modify wp_query by using Type Parameters

    $allsearch = &new WP_Query("s=$s&showposts=-1"); //what you currently have
    $customsearch = &new WP_Query("post_type=page&posts_per_page =5");
    

    This is how I believe you would filter post search

    if ( have_posts() ) : while ( have_posts() ) : the_post();
       if (is_page()) {} //do something if page
    endwhile;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b