Hướng dẫn chèn Google Search vào XenForo

nttruong

MasterCorporal
Tham gia
25/02/2015
Bài viết
368
Được Like
614
Để thêm thanh tìm kiếm Google vào xenforo, các bạn cần vào link sau http://www.google.com.vn/cse/ để đăng ký và get code.

Sau khi điền các thông tin cần thiết bạn sẽ nhận được một bảng như sau:
VuaCongNghe.Net-chengooglesearch.png

Các bạn lưu ý phần Search engine unique ID nhé

Tiếp theo các bạn mở template search_results thay toàn bộ code bằng đoạn sau:

Mã:
<xen:if is="{$search.searchConstraints.title_only}">
<xen:if is="{$search.search_query}">
<xen:title>{xen:phrase search_results_for_query}: {$search.search_query}</xen:title>
<xen:h1>{xen:phrase search_results_for_query}: <a href="{xen:link search, $search, 'searchform=1'}"><em>{$search.search_query}</em></a></xen:h1>
<xen:else />
<xen:title>{xen:phrase search_results}</xen:title>
</xen:if>

<xen:navigation>
<xen:breadcrumb href="{xen:link full:search}">{xen:phrase search}</xen:breadcrumb>
</xen:navigation>

<xen:container var="$head.robots">
<meta name="robots" content="noindex" /></xen:container>

<xen:require css="search_results.css" />

<xen:if is="{$search.searchWarnings}">
<ol class="searchWarnings">
<xen:foreach loop="$search.searchWarnings" value="$warning">
<li>{$warning}</li>
</xen:foreach>
</ol>
</xen:if>

<xen:if is="{$search.users}">
<xen:sidebar>
<div class="section userResults avatarList">
<div class="secondaryContent">
<h3>{xen:phrase matched_users}</h3>
<ul>
<xen:foreach loop="$search.users" value="$user">
<li class="userResult">
<xen:avatar user="$user" size="s" img="true" />
<xen:username user="$user" rich="true" />
<div class="userTitle">{xen:helper userTitle, $user}</div>
</li>
</xen:foreach>
</ul>
</div>
</div>
</xen:sidebar>
</xen:if>

<div class="pageNavLinkGroup">
<div class="linkGroup">
<a href="{xen:link search, $search, 'searchform=1'}">{xen:phrase search_again}</a>
</div>

<xen:pagenav link="search" linkdata="{$search}" page="{$page}" perpage="{$perPage}" total="{$totalResults}" />
</div>

<div class="section sectionMain searchResults">
<form action="" method="post">

<ol class="searchResultsList">
<xen:foreach loop="$results" value="$result" i="$i">
{xen:raw $result}
</xen:foreach>
</ol>

<xen:if is="{$userSearchMaxDate}">
<div class="secondaryContent olderMessages">
<a href="{xen:link search/member, '', 'user_id={$search.searchConstraints.user_id}', 'before={$userSearchMaxDate}'}">{xen:phrase find_older_messages}</a>
</div>
</xen:if>

<div class="sectionFooter searchResultSummary">
<span class="resultCount">{xen:phrase showing_results_x_to_y_of_z, 'start={xen:number $resultStartOffset}', 'end={xen:number $resultEndOffset}', 'total={xen:number $totalResults}'}</span>
<xen:if is="{$nextPage}"><a href="{xen:link search, $search, 'page={$nextPage}'}" class="nextLink">{xen:phrase next} &gt;</a></xen:if>
</div>

</form>
</div>

<div class="pageNavLinkGroup">
<div class="linkGroup">
<xen:if is="{$ignoredNames}">
<a href="javascript:" class="muted jsOnly DisplayIgnoredContent Tooltip" title="{xen:phrase show_hidden_content_by_x, "names={xen:helper implode, $ignoredNames, ', '}"}">{xen:phrase show_ignored_content}</a>
</xen:if>
<a href="{xen:link search, $search, 'searchform=1'}">{xen:phrase search_again}</a>
</div>

<xen:pagenav link="search" linkdata="{$search}" page="{$page}" perpage="{$perPage}" total="{$totalResults}" />
</div>
<xen:else />
<xen:if is="{$page} > 1">
<xen:if is="{$search.search_query}">
<xen:title>{xen:phrase search_results_for_query}: {$search.search_query}</xen:title>
<xen:h1>{xen:phrase search_results_for_query}: <a href="{xen:link search, $search, 'searchform=1'}"><em>{$search.search_query}</em></a></xen:h1>
<xen:else />
<xen:title>{xen:phrase search_results}</xen:title>
</xen:if>

<xen:navigation>
<xen:breadcrumb href="{xen:link full:search}">{xen:phrase search}</xen:breadcrumb>
</xen:navigation>

<xen:container var="$head.robots">
<meta name="robots" content="noindex" /></xen:container>

<xen:require css="search_results.css" />

<xen:if is="{$search.searchWarnings}">
<ol class="searchWarnings">
<xen:foreach loop="$search.searchWarnings" value="$warning">
<li>{$warning}</li>
</xen:foreach>
</ol>
</xen:if>

<xen:if is="{$search.users}">
<xen:sidebar>
<div class="section userResults avatarList">
<div class="secondaryContent">
<h3>{xen:phrase matched_users}</h3>
<ul>
<xen:foreach loop="$search.users" value="$user">
<li class="userResult">
<xen:avatar user="$user" size="s" img="true" />
<xen:username user="$user" rich="true" />
<div class="userTitle">{xen:helper userTitle, $user}</div>
</li>
</xen:foreach>
</ul>
</div>
</div>
</xen:sidebar>
</xen:if>

<div class="pageNavLinkGroup">
<div class="linkGroup">
<a href="{xen:link search, $search, 'searchform=1'}">{xen:phrase search_again}</a>
</div>

<xen:pagenav link="search" linkdata="{$search}" page="{$page}" perpage="{$perPage}" total="{$totalResults}" />
</div>

<div class="section sectionMain searchResults">
<form action="" method="post">

<ol class="searchResultsList">
<xen:foreach loop="$results" value="$result" i="$i">
{xen:raw $result}
</xen:foreach>
</ol>

<xen:if is="{$userSearchMaxDate}">
<div class="secondaryContent olderMessages">
<a href="{xen:link search/member, '', 'user_id={$search.searchConstraints.user_id}', 'before={$userSearchMaxDate}'}">{xen:phrase find_older_messages}</a>
</div>
</xen:if>

<div class="sectionFooter searchResultSummary">
<span class="resultCount">{xen:phrase showing_results_x_to_y_of_z, 'start={xen:number $resultStartOffset}', 'end={xen:number $resultEndOffset}', 'total={xen:number $totalResults}'}</span>
<xen:if is="{$nextPage}"><a href="{xen:link search, $search, 'page={$nextPage}'}" class="nextLink">{xen:phrase next} &gt;</a></xen:if>
</div>

</form>
</div>

<div class="pageNavLinkGroup">
<div class="linkGroup">
<xen:if is="{$ignoredNames}">
<a href="javascript:" class="muted jsOnly DisplayIgnoredContent Tooltip" title="{xen:phrase show_hidden_content_by_x, "names={xen:helper implode, $ignoredNames, ', '}"}">{xen:phrase show_ignored_content}</a>
</xen:if>
<a href="{xen:link search, $search, 'searchform=1'}">{xen:phrase search_again}</a>
</div>

<xen:pagenav link="search" linkdata="{$search}" page="{$page}" perpage="{$perPage}" total="{$totalResults}" />
</div>
<xen:else />
<xen:if is="{$search.search_query}">
<xen:title>{xen:phrase search_results_for_query}: {$search.search_query}</xen:title>
<xen:h1>{xen:phrase search_results_for_query}: <a href="{xen:link search, $search, 'searchform=1'}"><em>{$search.search_query}</em></a></xen:h1>
<xen:else />
<xen:title>{xen:phrase search_results}</xen:title>
</xen:if>

<ul class="tabs Tabs" data-panes="#damme_GoogleCustomSearch_Panes > li">
<li><a href="{xen:link search, $search}#gcs">Kết quả tìm kiếm từ Google</a></li>
<li><a href="{xen:link search, $search}#xf">Kết quả tìm kiếm từ địa phương</a></li>
</ul>

<br/>
<ul id="damme_GoogleCustomSearch_Panes">
<li id="gcs">
<div id="cse" style="padding: 10px; margin: 10px auto; border: 1px solid #a5cae4; -webkit-border-radius: 10px; -moz-border-radius: 10px; -khtml-border-radius: 10px;">Đang tải kết quả tìm kiếm từ Google ...</div>
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript">
            google.load('search', '1', {language : ''});
            google.setOnLoadCallback(function() {
                var customSearchOptions = {};
       
                var customSearchControl = new google.search.CustomSearchControl(
                    '013230238191179781022:tuf0bqxwzpe',
                    customSearchOptions
                );
                customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
       
                var options = new google.search.DrawOptions();
                options.enableSearchResultsOnly();
       
                customSearchControl.draw('cse', options);
                customSearchControl.execute('{$search.search_query}');
            }, true);
        </script>
</li>
<li id="xf">
<xen:navigation>
<xen:breadcrumb href="{xen:link full:search}">{xen:phrase search}</xen:breadcrumb>
</xen:navigation>

<xen:container var="$head.robots">
<meta name="robots" content="noindex" /></xen:container>

<xen:require css="search_results.css" />

<xen:if is="{$search.searchWarnings}">
<ol class="searchWarnings">
<xen:foreach loop="$search.searchWarnings" value="$warning">
<li>{$warning}</li>
</xen:foreach>
</ol>
</xen:if>

<xen:if is="{$search.users}">
<xen:sidebar>
<div class="section userResults avatarList">
<div class="secondaryContent">
<h3>{xen:phrase matched_users}</h3>
<ul>
<xen:foreach loop="$search.users" value="$user">
<li class="userResult">
<xen:avatar user="$user" size="s" img="true" />
<xen:username user="$user" rich="true" />
<div class="userTitle">{xen:helper userTitle, $user}</div>
</li>
</xen:foreach>
</ul>
</div>
</div>
</xen:sidebar>
</xen:if>

<div class="pageNavLinkGroup">
<div class="linkGroup">
<a href="{xen:link search, $search, 'searchform=1'}">{xen:phrase search_again}</a>
</div>

<xen:pagenav link="search" linkdata="{$search}" page="{$page}" perpage="{$perPage}" total="{$totalResults}" />
</div>

<div class="section sectionMain searchResults">
<form action="" method="post">

<ol class="searchResultsList">
<xen:foreach loop="$results" value="$result" i="$i">
{xen:raw $result}
</xen:foreach>
</ol>

<xen:if is="{$userSearchMaxDate}">
<div class="secondaryContent olderMessages">
<a href="{xen:link search/member, '', 'user_id={$search.searchConstraints.user_id}', 'before={$userSearchMaxDate}'}">{xen:phrase find_older_messages}</a>
</div>
</xen:if>

<div class="sectionFooter searchResultSummary">
<span class="resultCount">{xen:phrase showing_results_x_to_y_of_z, 'start={xen:number $resultStartOffset}', 'end={xen:number $resultEndOffset}', 'total={xen:number $totalResults}'}</span>
<xen:if is="{$nextPage}"><a href="{xen:link search, $search, 'page={$nextPage}'}" class="nextLink">{xen:phrase next} &gt;</a></xen:if>
</div>

</form>
</div>

<div class="pageNavLinkGroup">
<div class="linkGroup">
<xen:if is="{$ignoredNames}">
<a href="javascript:" class="muted jsOnly DisplayIgnoredContent Tooltip" title="{xen:phrase show_hidden_content_by_x, "names={xen:helper implode, $ignoredNames, ', '}"}">{xen:phrase show_ignored_content}</a>
</xen:if>
<a href="{xen:link search, $search, 'searchform=1'}">{xen:phrase search_again}</a>
</div>

<xen:pagenav link="search" linkdata="{$search}" page="{$page}" perpage="{$perPage}" total="{$totalResults}" />
</div>
</li>
</ul>
</xen:if>
</xen:if>

Ở đoạn trên các bạn lưu ý:

Mã:
var customSearchControl = new google.search.CustomSearchControl(
'018007349847158740104:3vjyrucanzs',
customSearchOptions
);

Thay đoạn 018007349847158740104:3vjyrucanzs bằng code Search engine unique ID bạn đã lấy được từ bước đăng ký.
Ảnh demo:

VuaCongNghe.Net-chengooglesearch1.png

Chúc các bạn thành công!
 
Sửa lần cuối bởi điều hành viên:

THB

Founder
Thành viên BQT
Tham gia
25/02/2015
Bài viết
6,651
Được Like
3,934
cái này hay.
 

anhtuan_vh3

Gefreiter
Tham gia
08/04/2015
Bài viết
73
Được Like
35
có bạn nào dùng cái này cho xen 1.5.1 chưa, mình dùng bị báo sai lỗi cú pháp :(. trước cũng dùng y như vậy cho xen 1.4 thì ko lỗi gì cả. giờ nâng cấp lên 1.5.1 thì báo lỗi
 

anhtuan_vh3

Gefreiter
Tham gia
08/04/2015
Bài viết
73
Được Like
35
Báo lỗi:
The following templates contained errors and were not saved: search_results: 1) Line 4: Template syntax error.
 

Chodongxoai

MasterCorporal
Tham gia
10/10/2015
Bài viết
218
Được Like
129
Cần gì phải thay vòng vo thế nhỉ? Tạo xong muốn để chổ nào thì ném vào đó thôi
 

Ha Trang2

Private
Tham gia
13/02/2016
Bài viết
3
Được Like
2
Đã làm thấy ngon nhưng nó không tìm trong resources thé mới lạ? Có cách nào khắc phục không các bác? Thanks!
 

Top Bottom