我已经从此问题安装了一个插件 - > Show most recent search terms in wordpress。我的插件正在我的网站上工作,但我想问一下,如何清除我网站上显示的最近搜索到的关键字。我正在想法如何实现它。如何清除最近搜索到的关键字在网站上显示 - 最近搜索小工具
正如你可以看到我的网站的结果:www.ncc.my。搜索框上方现在有10个关键字。我想清除所有关键字。我搜索了谷歌,但尚未得到答案。这是小部件的php代码。看看你是否可以得到解决方案来清除关键字。谢谢!
<?php
/*
Plugin Name: Recent Searches Widget
Plugin URI: http://www.poradnik-webmastera.com/projekty/recent_searches_widget/
Description: Shows recent searches in a sidebar widget.
Author: Daniel Frużyński
Version: 1.2
Author URI: http://www.poradnik-webmastera.com/
Text Domain: recent-searches-widget
*/
/* Copyright 2009-2010 Daniel Frużyński (email : daniel [A-T] poradnik-webmastera.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if (!class_exists('RecentSearchesWidget')) {
class RecentSearchesWidget {
// Constructor
function RecentSearchesWidget() {
// Initialize plugin
add_action('init', array(&$this, 'init'));
// Page load
add_action('template_redirect', array(&$this, 'template_redirect'));
// Widgets initialization
add_action('widgets_init', array(&$this, 'widgets_init'));
}
// Plugin initialization
function init() {
load_plugin_textdomain('recent-searches-widget', false, dirname(plugin_basename(__FILE__)) . '/lang');
}
// Page load
function template_redirect() {
if (is_search()) {
// Store search term
$query = $this->strtolower(trim(get_search_query()));
$options = get_option('recent_searches_widget');
if (!is_array($options)) {
$options = $this->get_default_options();
}
$max = $options['max'];
$data = get_option('recent_searches_widget_data', array());
if (!is_array($data)) {
if (isset($options['data'])) {
$data = $options['data'];
unset($options['data']);
update_option('recent_searches_widget', $options);
}
if (!is_array($data)) {
$data = array();
}
}
$pos = array_search($query, $data);
if ($pos !== false) {
if ($pos != 0) {
$data = array_merge(array_slice($data, 0, $pos),
array($query), array_slice($data, $pos + 1));
}
} else {
array_unshift($data, $query);
if (count($data) > $max) {
array_pop($data);
}
}
update_option('recent_searches_widget_data', $data);
}
}
// Widgets initialization
function widgets_init() {
$widget_ops = array(
'classname' => 'widget_rsw',
'description' => __('Shows recent searches', 'recent-searches-widget'),
);
wp_register_sidebar_widget('recentsearcheswidget', __('Recent Searches', 'recent-searches-widget'),
array(&$this, 'widget_rsw'), $widget_ops);
wp_register_widget_control('recentsearcheswidget', __('Recent Searches', 'recent-searches-widget'),
array(&$this, 'widget_rsw_control'));
}
function widget_rsw($args) {
extract($args);
$title = isset($options['title']) ? $options['title'] : '';
$title = apply_filters('widget_title', $title);
if (empty($title))
$title = ' ';
echo $before_widget . $before_title . $title . $after_title, "\n";
$this->show_recent_searches("<ul>\n<li>", "</li>\n</ul>", "</li>\n<li>");
echo $after_widget;
}
function show_recent_searches($before_list, $after_list, $between_items) {
$options = get_option('recent_searches_widget');
if (!is_array($options)) {
$options = $this->get_default_options();
}
$data = get_option('recent_searches_widget_data');
if (!is_array($data)) {
if (isset($options['data'])) {
$data = $options['data'];
}
if (!is_array($data)) {
$data = array();
}
}
if (count($data) > 0) {
echo $before_list;
$first = true;
foreach ($data as $search) {
if ($first) {
$first = false;
} else {
echo $between_items;
}
echo '<a href="', get_search_link($search), '"';
if ($options['nofollow']) {
echo ' rel="nofollow"';
}
echo '>', wp_specialchars($search), '</a>';
}
echo $after_list, "\n";
} else {
_e('No searches yet', 'recent-searches-widget');
}
}
function widget_rsw_control() {
$options = $newoptions = get_option('recent_searches_widget', array());
if (count($options) == 0) {
$options = $this->get_default_options();
update_option('recent_searches_widget', $options);
}
if (isset($_POST['rsw-submit'])) {
$options['title'] = strip_tags(stripslashes($_POST['rsw-title']));
$options['max'] = (int)($_POST['rsw-max']);
$options['nofollow'] = isset($_POST['rsw-nofollow']);
if (count($options['data']) > $options['max']) {
$options['data'] = array_slice($options['data'], 0, $options['max']);
}
update_option('recent_searches_widget', $options);
}
$title = attribute_escape($options['title']);
$max = attribute_escape($options['max']);
$nofollow = $options['nofollow'];
?>
<p><label for="rsw-title"><?php _e('Title:', 'recent-searches-widget'); ?> <input class="widefat" id="rsw-title" name="rsw-title" type="text" value="<?php echo $title; ?>" /></label></p>
<p><label for="rsw-max"><?php _e('Max searches:', 'recent-searches-widget'); ?> <input id="rsw-max" name="rsw-max" type="text" size="3" maxlength="5" value="<?php echo $max; ?>" /></label></p>
<p><label for="rsw-nofollow"><?php _e('Add <code>rel="nofollow"</code> to links:', 'recent-searches-widget'); ?> <input id="rsw-nofollow" name="rsw-nofollow" type="checkbox" value="yes" <?php checked($nofollow, true); ?>" /></label></p>
<input type="hidden" id="rsw-submit" name="rsw-submit" value="1" />
<?php
}
// Make string lowercase
function strtolower($str) {
if (function_exists('mb_strtolower')) {
return mb_strtolower($str);
} else {
return strtolower($str);
}
}
function get_default_options() {
return array(
'title' => '',
'max' => 4,
'nofollow' => true,
);
}
}
// Add functions from WP2.8 for previous WP versions
if (!function_exists('esc_html')) {
function esc_html($text) {
return wp_specialchars($text);
}
}
if (!function_exists('esc_attr')) {
function esc_attr($text) {
return attribute_escape($text);
}
}
// Add functions from WP3.0 for previous WP versions
if (!function_exists('get_search_link')) {
function get_search_link($query = '') {
global $wp_rewrite;
if (empty($query))
$search = get_search_query();
else
$search = stripslashes($query);
$permastruct = $wp_rewrite->get_search_permastruct();
if (empty($permastruct)) {
$link = home_url('?s=' . urlencode($search));
} else {
$search = urlencode($search);
$search = str_replace('%2F', '/', $search); // %2F(/) is not valid within a URL, send it unencoded.
$link = str_replace('%search%', $search, $permastruct);
$link = trailingslashit(get_option('home')) . user_trailingslashit($link, 'search');
}
return apply_filters('search_link', $link, $search);
}
}
$wp_recent_searches_widget = new RecentSearchesWidget();
// Show recent searches anywhere in the theme
function rsw_show_recent_searches($before_list = "<ul>\n<li>", $after_list = "</li>\n</ul>", $between_items = "</li>\n<li>") {
global $wp_recent_searches_widget;
$wp_recent_searches_widget->show_recent_searches($before_list, $after_list, $between_items);
}
} // END
?>
我看不到清除关键字的部分。任何建议?谢谢!
更新部分: 清除历史记录问题解决后,这是我得到的下一个问题。
function get_default_options() {
return array(
'title' => '',
'max' => 4, <---it was originally set to 10
'nofollow' => true,
);
}
}
,我设置了搜索的关键字为“4”,它最初被设置为10,它应该工作,并通过正确的只显示最多4个关键字。但是,我不知道为什么这个设置似乎跟着我第一次使用这个插件。无论我如何设置从0到5,该设置都不会生效,并且关键字仍然显示为最多10次搜索。需要帮助!
任何解决方案?
嗨Tabris963,非常感谢你!有效。但是,现在我在同一个问题上面临着不同的问题。现在,我将搜索到的关键字数量设置为“4”。正确的,它应该只显示我的网站上4个搜索。但是,我不知道为什么无论我设置什么都不起作用。此问题已更新。你可以看看,以便知道哪一部分。再次感谢! – Jornes 2015-02-25 02:11:47
我试图找到一种方式,但由于某种原因,当我应用更改别的东西(我无法控制)似乎应用于(例如,它开始以大写字母显示所有内容或它改变了当他想要的元素(这不是我正在寻找的解决方案,因为我不能“复制”它))... – tabris963 2015-02-25 10:24:21
嗨Tabris963,谢谢!希望你能找到解决这个问题的方法。我真的很需要这个插件,只有三个类似的插件,这是最好的,因为其余的都是没有指示的。不知道如何配置和使用。你知道一个名为“最近的搜索”的插件吗?没有说明如何配置。你知道吗? :) – Jornes 2015-02-25 11:02:03