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

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

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

首先说一下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'];
}

最后保存修改就可以了。

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

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

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

分享给朋友:

相关文章

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

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

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

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

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

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

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

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

Godaddy空间绑定多个域名的方法

1,先到 Domain names-->off-site DNS2,点击add domains3,写入你要加入绑定的域名4,等待系统设定完成5,回到主机管理6,点击主机的OPEN7,点击别名设定8,点击加入别名9,选择你刚刚加入的域名...

如何开通GoDaddy的免费空间

如何开通GoDaddy的免费空间

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

发表评论

访客

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