当前位置:首页 > 计算机相关 > 建站之路 > 正文内容

wordpress友情链接模块只在首页显示的方法

piikee3年前 (2020-04-26)建站之路532

首先说一下wordpress友情链接模块怎么添加。

先登录wordpress后台,然后 外观->小工具->找到【文本】这个小工具,拉到右边 【侧边栏】里面,如下图:



然后点击那个小三角,输入【友情链接】,如下图:



搞定,下面就可以输入你要加的友情链接html代码了。

接下来这个会整站侧边栏显示,我们来让他只显示在首页。

找到/wp-includes/widgets/class-wp-widget-text.php中以下代码

public function widget( $args, $instance ) {
global $post;

在后面插入:

  if(!is_home()&&$instance['title']=="友情链接") //这里注意,你上面文本标题写友情链接,这里就是友情链接,如果写的是其他的比如,ABC,那么这里就是=="ABC"。这句就是根据文本模块的标题判断是否要隐藏这个模块。
return;

最后整个函数的样子如下:

public function widget( $args, $instance ) {
global $post;
if(!is_home()&&$instance['title']=="友情链接")
return;
$title = ! empty( $instance['title'] ) ? $instance['title'] : '';
/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
$text = ! empty( $instance['text'] ) ? $instance['text'] : '';
$is_visual_text_widget = ( ! empty( $instance['visual'] ) && ! empty( $instance['filter'] ) );
// In 4.8.0 only, visual Text widgets get filter=content, without visual prop; upgrade instance props just-in-time.
if ( ! $is_visual_text_widget ) {
$is_visual_text_widget = ( isset( $instance['filter'] ) && 'content' === $instance['filter'] );
}
if ( $is_visual_text_widget ) {
$instance['filter'] = true;
$instance['visual'] = true;
}
/*
* Suspend legacy plugin-supplied do_shortcode() for 'widget_text' filter for the visual Text widget to prevent
* shortcodes being processed twice. Now do_shortcode() is added to the 'widget_text_content' filter in core itself
* and it applies after wpautop() to prevent corrupting HTML output added by the shortcode. When do_shortcode() is
* added to 'widget_text_content' then do_shortcode() will be manually called when in legacy mode as well.
*/
$widget_text_do_shortcode_priority = has_filter( 'widget_text', 'do_shortcode' );
$should_suspend_legacy_shortcode_support = ( $is_visual_text_widget && false !== $widget_text_do_shortcode_priority );
if ( $should_suspend_legacy_shortcode_support ) {
remove_filter( 'widget_text', 'do_shortcode', $widget_text_do_shortcode_priority );
}
// Override global $post so filters (and shortcodes) apply in a consistent context.
$original_post = $post;
if ( is_singular() ) {
// Make sure post is always the queried object on singular queries (not from another sub-query that failed to clean up the global $post).
$post = get_queried_object();
} else {
// Nullify the $post global during widget rendering to prevent shortcodes from running with the unexpected context on archive queries.
$post = null;
}
// Prevent dumping out all attachments from the media library.
add_filter( 'shortcode_atts_gallery', array( $this, '_filter_gallery_shortcode_attrs' ) );
/**
* Filters the content of the Text widget.
*
* @since 2.3.0
* @since 4.4.0 Added the `$this` parameter.
* @since 4.8.1 The `$this` param may now be a `WP_Widget_Custom_HTML` object in addition to a `WP_Widget_Text` object.
*
* @param string $text The widget content.
* @param array $instance Array of settings for the current widget.
* @param WP_Widget_Text|WP_Widget_Custom_HTML $this Current Text widget instance.
*/
$text = apply_filters( 'widget_text', $text, $instance, $this );
if ( $is_visual_text_widget ) {
/**
* Filters the content of the Text widget to apply changes expected from the visual (TinyMCE) editor.
*
* By default a subset of the_content filters are applied, including wpautop and wptexturize.
*
* @since 4.8.0
*
* @param string $text The widget content.
* @param array $instance Array of settings for the current widget.
* @param WP_Widget_Text $this Current Text widget instance.
*/
$text = apply_filters( 'widget_text_content', $text, $instance, $this );
} else {
// Now in legacy mode, add paragraphs and line breaks when checkbox is checked.
if ( ! empty( $instance['filter'] ) ) {
$text = wpautop( $text );
}
/*
* Manually do shortcodes on the content when the core-added filter is present. It is added by default
* in core by adding do_shortcode() to the 'widget_text_content' filter to apply after wpautop().
* Since the legacy Text widget runs wpautop() after 'widget_text' filters are applied, the widget in
* legacy mode here manually applies do_shortcode() on the content unless the default
* core filter for 'widget_text_content' has been removed, or if do_shortcode() has already
* been applied via a plugin adding do_shortcode() to 'widget_text' filters.
*/
if ( has_filter( 'widget_text_content', 'do_shortcode' ) && ! $widget_text_do_shortcode_priority ) {
if ( ! empty( $instance['filter'] ) ) {
$text = shortcode_unautop( $text );
}
$text = do_shortcode( $text );
}
}
// Restore post global.
$post = $original_post;
remove_filter( 'shortcode_atts_gallery', array( $this, '_filter_gallery_shortcode_attrs' ) );
// Undo suspension of legacy plugin-supplied shortcode handling.
if ( $should_suspend_legacy_shortcode_support ) {
add_filter( 'widget_text', 'do_shortcode', $widget_text_do_shortcode_priority );
}
echo $args['before_widget'];
if ( ! empty( $title ) ) {
echo $args['before_title'] . $title . $args['after_title'];
}
$text = preg_replace_callback( '#<(video|iframe|object|embed)\s[^>]*>#i', array( $this, 'inject_video_max_width_style' ), $text );
// Adds noreferrer and noopener relationships, without duplicating values, to all HTML A elements that have a target.
$text = wp_targeted_link_rel( $text );
?>
<div class="textwidget"><?php echo $text; ?></div>
<?php
echo $args['after_widget'];
}

最后保存修改就可以了。

扫描二维码推送至手机访问。

版权声明:本文由萍客小居发布,如需转载请注明出处。

本文链接:https://www.piikee.net/1386.html

分享给朋友:

相关文章

bo blog出错

死人了,要考试,居然还一天没复习,刚刚想复习,却看到博客有新版,结果升级新版出现问题,备份数据库,重新全新安装,结果,数据库字段出错无法导入备份啊啊啊疯了!好不容易,才找到出错字段,原来是以前加入一个博文自动发QQ空间的插件,结果修改了数据...

关于网站ani格式鼠标样式无法显示的问题

通过CSS样式,可以使鼠标变成我们所要的图片形式。但是,有一些站长,用了.ani格式的鼠标图片,在本地调试成功,但上传到了服务器,就无法显示出鼠标样式来了。其实,这是由于服务器系统的问题造成的。Windows2003的服务器是不支持.ani...

几个软件站采集规则(新云4.0系统测试通过)

霏凡软件站采集规则列表 http://www.crsky.com/list/r_3_1.html获取列表开始代码: <li><a class="current" href="#"><span>按时间排序&...

改变新云4.0首页默认菜单

新云系统的首页默认菜单式是下载频道的,如果要改变为文章频道菜单,可以到包含文件中的header.html里面,找到[code]<li class="current"><a href="/" target="_top">...

AdSense 广告在 Discuz! 论坛广告展示问题及解决办法

请进入Discuz!安装目录,在include\js目录下的common.js文件内删除以下语句:Array.prototype.push = function(value) {this[this.length] = value;retur...

Google AdSense 最新申请方法汇总

关键是 收款人 ,一个人名不能多帐户,收钱时必须真实地址,同一个人同一个地址多个账号很危险。所以用亲戚朋友姓名申请,一个人一个号,申请时地址可以变化,以后可以改。也就是说,只要有个收款人名字就行了,地址信息与网站匹配!方法1  [推荐]利用...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。