wordpress中短代码失效解决办法
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>在以前的代码中,我们仅仅使用add_shortcode函数来把短代码注册到wordpress中,在文章页使用the_content来显示文章内容,这样短代码就能显示成为我们想要的效果。但是,正如前文所述,这种方法失效了,用the_content来使短代码生效的愿望破灭,不过我们可以对此进行修改,以此再让它生效。用下面的代码替换你原来主题中的the_content();</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
</p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
<p>
代码如下:</p>
</div>
<div id="phpcode3" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
$content = apply_filters( 'the_content',get_the_content());<br>
echo do_shortcode( $content );</div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
这段代码即可让短代码生效,原理也很简单,先通过apply_filter让文章的内容格式化,这个效果跟the_content的效果基本相同,只不过以字符串的形式返还,再通过do_shortcode使得$content中的短代码也生效,这样就可以实现我们想要的效果。</p>
頁:
[1]