gpt4 book ai didi

wordpress - 如何从 WordPress 简码结果中删除多余的


转载 作者:行者123 更新时间:2023-12-02 10:20:57 24 4
gpt4 key购买 nike

我使用shortcode在变量中输出以下html,但是有太多冗余代码,例如br和p标签,如何删除它们?谢谢!

我的简码功能:

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>

最佳答案

尝试在显示内容之前添加此代码:

已更新

// From your question 
$content = do_shortcode($content);
// Put the original shortcodes back
$shortcode_tags = $orig_shortcode_tags;

// Add this code
$content = preg_replace( '%<p>&nbsp;\s*</p>%', '', $content ); // Remove all instances of "<p>&nbsp;</p>" to avoid extra lines.
$Old = array( '<br />', '<br>' );
$New = array( '','' );
$content = str_replace( $Old, $New, $content );

// From your question
return $content;

关于wordpress - 如何从 WordPress 简码结果中删除多余的 <p> 和 <br/>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14016007/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com