4

我用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>
4

1 回答 1

4

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

更新

// 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;
于 2012-12-24T01:10:38.080 回答