WordPress教程

隐藏失效的 WordPress 短代码

阿里云

很多 WordPress 主题插件都集成了独有的短代码,从则实现一些功能,但有朝一日换主题或停用后,短代码失效,只显示一段代码,可以通过下面的方法隐藏这些失效的 WordPress 短代码。

section

例如原添加短代码代码为:

也想出现在这里?联系我们
创客主机
  1.     add_shortcode('my-shortcode', 'my_shortcode_function');

想隐藏这个短代码,将下面的代码添加到当前主题函数模板 functions.php 中即可。

  1.     add_shortcode( 'my-shortcode', '__return_false' );

其中的 my-shortcode 为原短代码名称。

section

如果想删除这些短代码,在众多文章中逐个打开查找,太麻烦,可以用下面的代码批量搜索哪些文章添加了某个短代码。

将代码添加到当前主题函数模板 functions.php 中:

  1.     function zm_find_shortcode( $atts, $content=null ) { 
  2.     	ob_start();
  3.     	extract( shortcode_atts( array(
  4.     		'find' => '',
  5.     		), $atts ) );
  6.  
  7.     	$string = $atts['find'];
  8.     	$args = array(
  9.     		's' => $string,
  10.     		);
  11.  
  12.     	$the_query = new WP_Query( $args );
  13.     	if ( $the_query->have_posts() ) {
  14.     		echo '<ul>';
  15.     		while ( $the_query->have_posts() ) {
  16.     			$the_query->the_post(); ?>
  17.     			<li><a href="<?php  the_permalink() ?>"><?php the_title(); ?></a></li>
  18.     			<?php
  19.     		}
  20.     		echo '</ul>';
  21.     	} else {
  22.     		echo "Sorry no posts found"; 
  23.     	}
  24.     	wp_reset_postdata();
  25.     	return ob_get_clean();
  26.     }
  27.     add_shortcode( 'shortcodefinder', 'zm_find_shortcode' );

发表一篇文章,并添加短代码:

  1.    [shortcodefinder find='短代码名称']

之后会在这篇文章中列出所有添加短代码的文章。

这段搜索短代码文章的方法还有其它用处,简单改一下,可以调用指定的文章,比如调用添加过下载按钮短代码的文章等。

section

还可以安装短代码查找插件:Shortcodes Finder

通过插件不仅可以找到有短代码的文章,还可以查看短代码的源代码位置,并可以编辑。

隐藏失效的 WordPress 短代码

已有 167 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!