WordPress 外掛:顯示搜尋結果數目

發布於:08 年 11 月 17 日 | 類別:WordPress

一目瞭然的搜尋結果
使用 WordPress 有一小段的時間,對於 WordPress 所提供的功能和擴充性一直都非常讚賞,只是有一個小地方總是覺得可以更好:搜尋結果。可能已經習慣了 Google 搜尋引擎所提供的搜尋結果方式:會顯示出目前找到幾筆資料、目前頁面顯示是第幾筆,讓使用者可以第一時間知道搜尋結果的大概範圍。

若要讓 WordPress 的搜尋功能也能像 Google 一樣顯示找到幾筆資料,則可以利用「Results Count Remix」外掛來達成。實際範例則可以透過本網誌右邊的搜尋功能,搜尋看看所呈現出來的結果。

如何安裝「Results Count Remix」外掛?
下載「Results Count Remix」解壓縮後,上傳至 plugins 資料夾,再到 WordPress 後台啟用外掛即可。

如何設定「Results Count Remix」外掛?
可以將下面的語法,加到下面建議的檔案中:index.php、archive.php、date.php、category.php、tag.php、search.php;而加入位置可以依自己想要顯示的地方而定。

1
<?php if (function_exists('results_count_remix')) results_count_remix(); ?>


以本網誌搜尋「WordPress」,最原始的搜尋結果會長這個樣子:

最原始的搜尋結果:以搜尋「WordPress」為例

最原始的搜尋結果:以搜尋「WordPress」為例


修改搜尋結果樣式
如果對原始預設的搜尋結果不滿意的話,還可以透過更改外掛的原始檔 (results-count-remix.php) 來個人化,像是顯示得跟 Google 搜尋結果一樣:「 約有 236,000,000 項符合 wordpress 的查詢結果,以下是第 1-10 項。」

修改過的「搜尋結果」顯示:以搜尋 WordPress 為例

修改過的「搜尋結果」顯示:以搜尋 WordPress 為例


以修改「搜尋結果」為例,開啟 results-count-remix.php 檔案,將原來 #119 行到 #134 行的程式碼用下面的語法覆蓋:(由於程式碼內含有中文,必須將格式儲存成 Encode in UTF-8 without BOM)

119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Handle search results
elseif (is_search()) {
	if ($plural_pages)
		//echo '<p>', $startpost, '&ndash;', $endpost,' of ', $numposts, ' results for the search query:</p>';
		echo '<div class="result_count">共有 <strong>', $numposts, '</strong> 項符合 <strong>', attribute_escape(get_search_query()), '</strong> 的查詢結果,以下是第 <strong>', $startpost, '&ndash;', $endpost, '</strong> 項。</div>';
	else {
		if ($plural_posts)
			//echo '<p>', $numposts, ' results for the search query:</p>';
			echo '<div class="result_count">共有 <strong>', $numposts, '</strong> 項符合 <strong>', attribute_escape(get_search_query()), '</strong> 的查詢結果,以下是第 <strong>', $startpost, '&ndash;', $endpost, '</strong> 項。</div>';
		else
			//echo '<p>One result for the search query:</p>';
			echo '<div class="result_count">共有 <strong>', $numposts, '</strong> 項符合 <strong>', attribute_escape(get_search_query()), '</strong> 的查詢結果,以下是第 <strong>', $startpost, '&ndash;', $endpost, '</strong> 項。</div>';
	}
 
	//echo '<h1>', attribute_escape(get_search_query()), '</h1>';
}


然後在自己的 style.css 內加上「result_count」這個 class 即可。

1
2
3
4
5
.result_count {
	background: #F3F3F3 none repeat scroll 0 0;
	margin-bottom: 12px;
	padding: 2px 0 2px 7px;
}


除了搜尋結果之外
除了讓 WordPress 顯示搜尋結果的數目之外,此外掛還可以支援:「文章標籤、文章類別、文章彙整」,一樣也是可以讓使用者很清楚的知道「目前這個標籤有幾篇文章、目前是第幾篇等資訊」。修改的方式則和「搜尋結果」一樣,如果懶的一個一個改,則可以直接下載「修改過後的版本」。

其他應用:文章標籤、文章類別、文章彙整

其他應用:文章標籤、文章類別、文章彙整


連結整理

文章標籤


相關文章

分享文章至:

開始留言

(完成後可按 Ctrl+Enter 發布)