我使用shortcode在一个变量中输出以下html,但有太多冗余代码,如br和p标记,如何删除它们?谢谢
HTML组成
$return_html.=\'<div class="portfolio-item" id="portfolio-\'.$post->ID.\'"><a class="overlay\'.$class.\'" href="\'.$url.\'" title="\'.$post->post_title.\'"><h3>\'.$post->post_title.\'</h3><p class="intro">\'.van_truncate($post->post_content,100).\'</p></a><div class="tools"><a href="\'.$url.\'" class="zoomin\'.$class.\'" title="\'.$post->post_title.\'">ZoomIn</a><a href="\'.get_permalink($post->ID).\'" class="info">Info</a></div>
<a href="\'.get_permalink($post->ID).\'" class="item"><img src="\'.get_template_directory_uri().\'/images/pixel.gif" data-url="\'.$thumbnail_url[0].\'" class="scrollLoading" /></a></div>\';
当前短码函数
add_shortcode(\'portfolios\', \'van_portfolios_shortcode\');
function van_portfolios_shortcode( $atts, $content) {
extract(shortcode_atts(array(
\'number\'=>\'9\',
\'slug\'=>\'\'
), $atts));
$str=van_portfolios($slug,$number,false);
return $str;
}
function van_process_shortcode($content) {
global $shortcode_tags;
// Backup current registered shortcodes and clear them all out
$orig_shortcode_tags = $shortcode_tags;
$shortcode_tags = array();
add_shortcode(\'portfolios\', \'van_portfolios_shortcode\');
// Do the shortcode (only the one above is registered)
$content = do_shortcode($content);
// Put the original shortcodes back
$shortcode_tags = $orig_shortcode_tags;
return $content;
}
add_filter(\'the_content\', \'van_process_shortcode\', 7);
正确的化妆是
<div class="portfolio-item">
<a class="overlay" href="#">
<h3>...</h3>
<p>...</p>
</a>
<div class="tools"><a href="#" class="zoomin" rel="lightbox">ZoomIn</a><a href="#" class="info">Info</a></div>
<a href="#" class="item">...</a>
</div>
输出:
<div class="portfolio-item">
<a class="overlay" href="#">
<br /><!--This <br />is redundant code-->
<h3>...</h3>
<p>...</p><p><!--This <p> is redundant code-->
</a>
<div class="tools"><a href="#" class="zoomin" rel="lightbox">ZoomIn</a><a href="#" class="info">Info</a></div>
<p><!--This <p> is redundant code--><a href="#" class="item">...</a>
</div>
SO网友:shea
这是因为帖子的内容,以及你的短代码,都是通过wpautop()
生成<p>
和<br />
标签,以便正确隔开立柱。
短代码以优先级11运行,该优先级在wpautop()
优先级为10。我们可以更改之前运行此短代码的优先级wpautop()
:
// This will do nothing but will allow the shortcode to be stripped
add_shortcode( \'foobar\', \'shortcode_foobar\' );
// Actual processing of the shortcode happens here
function foobar_run_shortcode( $content ) {
global $shortcode_tags;
// Backup current registered shortcodes and clear them all out
$orig_shortcode_tags = $shortcode_tags;
remove_all_shortcodes();
add_shortcode( \'foobar\', \'shortcode_foobar\' );
// Do the shortcode (only the one above is registered)
$content = do_shortcode( $content );
// Put the original shortcodes back
$shortcode_tags = $orig_shortcode_tags;
return $content;
}
add_filter( \'the_content\', \'foobar_run_shortcode\', 7 );
代码感谢
@Viper007Bond [source]。记住用与您的短代码相关的名称替换函数和过滤器名称!