开发者

Wordpress Custom Search Form within a page

开发者 https://www.devze.com 2023-04-09 03:02 出处:网络
I need to add a custom search form within a page, and the search form results should return/highlight items within that page..

I need to add a custom search form within a page, and the search form results should return/highlight items within that page..

ex..

in a page: monkey pig tree

when a user searches for 'pig'

it'll either display only 'pig' or highlight the word pig.

I thought of using custom query but I realized it'll display the whole content inste开发者_Go百科ad of the actual text/searched item itself.


You can highlight your search results with a few simple steps. For this solution your theme needs to have a search.php file. If it does, follow these simple steps:

Open the search.php file and find <?php the_title(); ?>

Replace that with this:

<?php echo $title; ?>

Now you'll need to place this bit of code before the title tag you just changed (anywhere before should work, just be sure not to place it inside an open

<?php 
  $title = get_the_title(); 
  $keys= explode(" ",$s); 
  $title = preg_replace('/('.implode('|', $keys) .')/iu', '<strong class="search-keywords">\0</strong>', 
  $title); 
?>

What that code does is add a class to the title or in this case the results of the search. The class in my example is .search-keywords. Now you need to open your themes style.css file and add some styles to it. Here is an example (change it however you need:

.search-keywords {
  background:yellow;
  color:blue;
  }
0

精彩评论

暂无评论...
验证码 换一张
取 消