Search Term Highlighting


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <div class="post">
  2. <?php $title = get_the_title(); $keys= explode(" ",$s); $title = preg_replace('/('.implode('|', $keys) .')/iu', '<strong class="search-excerpt-title">\0</strong>', $title); ?>
  3.  
  4. <h2 class="title"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php echo $title; ?></a></h2>
  5.  
  6. <p class="post-meta">
  7. <!-- <span class="comments"><?php comments_popup_link(__('0 Comments', 'woothemes'), __('1 Comment', 'woothemes'), __('% Comments', 'woothemes')); ?></span> -->
  8. </p>
  9.  
  10. <div class="entry">
  11.  
  12. <!-- Search Term Highlighting in the excerpt -->
  13. <?php $excerpt = get_the_excerpt(); $keys= explode(" ",$s); $excerpt = preg_replace('/('.implode('|', $keys) .')/iu', '<strong class="search-excerpt">\0</strong>', $excerpt); ?>
  14.  
  15.  
  16. <?php echo $excerpt; ?>
  17. </div><!-- /.entry -->

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.