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

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

piikee4年前 (2020-04-26)建站之路875

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

分享给朋友:

相关文章

关于PHP的前途

1.序言定义:PHP是一种简单的,面向对象的,解释型的,健壮的,安全的,性能非常之高的,独立于架构的,可移植的,动态的脚本语言。PHP具有和JAVA类似的Class关键字。因为不需要虚拟机,以致速度比JAVA快5倍。PHP正迅速变成一种标准...

改变新云4.0首页默认菜单

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

再爆腾讯QQ表情栏目采集规则

站点URL: http://im.qq.com采集列表URL: http://im.qq.com/cgi-bin/face/face_sort?type=1获取列表开始代码:<table width="100%" border="0"...

DEDECMS淘宝客合作版无法采集商品的原因

DEDECMS的淘宝客合作版,看起来很不错,结果弄了几天,一直无法采集。本地调试还可以的,上传到空间就无法采集了,每次点击展开,选择分类商品之后,页面就空白没反应了。最后才发现,原来是服务器的PHP版本太旧了!!!登录DEDECMS的后台,...

asp站点com1.gupiao.asp,prn.gupiao.asp木马清除方法

很不幸,不知什么时候网站被挂马了!网站的跟目录被加了几个文件,怎么删也删不掉,而且过一段时间还会自动在 ASP 的网站首页插一段<div class=sera><ul><li style='float:left...

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

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

发表评论

访客

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