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

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

piikee2年前 (2020-04-26)建站之路316

首先说一下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

分享给朋友:

相关文章

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

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

血染的互联网 全国封网祭

也许我被关,将不再回来你是否理解,你是否明白也许我倒下,将不再起来你是否还要永久的期待如果是这样,你不要悲哀互联网的旗帜上有我们血染的风采如果是这样,你不要悲哀互联网的旗帜上有我们血染的风采也许我的页面,再不能打开你是否理解我沉默的情怀也许...

PHP168 B2B系统 自定义 MYSQL万能标签

PHP168很强悍,但是标签还不是很完善。最近弄首页标签,有些标签模块都没出效果,只好自己研究MYSQL万能标签了。弄个日志来记记这些标签吧。招聘标签:最新10个企业招聘信息(获得id,企业名,发布时间)MYSQLSELECT jobs_i...

DEDECMS首页加入滚动公告栏的方法

近期新上线网站,使用了DEDECMS的系统。由于首页需要加入滚动站内新闻,于是搜了一下,都找不到好的代码。想起自己前期开发的一个JS广告滚动代码,决定自己开发一个功能来。经过一个下午的努力,总算给弄了出来。不多说,直接上代码。总共有三部分代...

如何开通GoDaddy的免费空间

如何开通GoDaddy的免费空间

只要在 GoDaddy 注册了域名,就可以得到一个10G的免费空间。很多朋友在注册域名后,不知道如何开通GoDaddy的10G免费空间,下面就一步一步地介绍 GoDaddy的免费空间的开通方法。(1). 点击 My Account,登录帐号...

netfirms免费空间去广告代码

netfirms免费空间头部都有一个广告条,把下面代码放入body 里面即可实现隐藏广告条。<script><!-- 转载请注明来自萍客小居www.piikee.net -->var table1 = documen...

发表评论

访客

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