如何消除冗余<;p>;及<;br/>;根据WordPress短代码的结果

如何消除冗余<;p>;及<;br/>;根据WordPress短代码的结果,wordpress,shortcode,Wordpress,Shortcode,我使用短代码在一个变量中输出以下html,但是有太多的冗余代码,例如br和p标记,如何删除它们?谢谢 我的快捷码功能: add_shortcode('portfolios', 'van_portfolios_shortcode'); function van_portfolios_shortcode( $atts, $content) { extract(shortcode_atts(array( 'number'=>'9', 'slug'=>

我使用短代码在一个变量中输出以下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="#">
             <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>
正确的化妆是

<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>

输出:

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="#">
             <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;
//根据您的问题
$content=do_短代码($content);
//将原来的短代码放回原处
$shortcode\u tags=$orig\u shortcode\u tags;
//添加此代码
$content=preg_replace('%\s*

%',''$content);//删除“

”的所有实例以避免多余的行。 $Old=数组('
','
'); $New=数组(“”,”); $content=str_replace($Old,$New,$content); //从你的问题 返回$content;
您可以在纸上打印出每个HTML页面,然后使用某种文本标记器标记每个

,并使用锋利的剪刀将其剪下。我尝试了您的方法,但仍然生成
,其他短代码无效。@BadJohnny更新了我的答案。非常感谢。受您的代码启发,我刚刚将_content()替换为echo do_短代码(get_the_content()),问题解决了!再次感谢你。