如何在没有P标签包装的情况下回显the_excerpt?


11

在下面的代码片段中,我试图使the_excerpt被写出而没有标签。但是,源格式显示the_excerpt始终包装在P标签中。如何删除没有标签的摘录?

foreach($myrecentposts as  $idxrecent=>$post) 
{ ?>
<li class="page_item">
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    <?php echo strip_tags(substr( the_excerpt(), 0, 75 ))."..." ?>
</li><?php }    
echo "</ul>
</div>";}

Answers:


13

在上面的代码中使用get_the_excerpt()而不是the_excerpt(),因为最后一个将摘录输出到屏幕,而不将其传递给其他函数...


7

如何删除wpautop列表前的过滤器?

remove_filter( 'the_excerpt', 'wpautop' );

(请确保稍后再添加它,以免弄乱其他格式...)


这是正确的答案,将特别删除您用于输出内容的特定页面上的格式。
查尔斯

0

我尝试了上述答案,但对我没有用。

我尝试使用the_excerpt,但未显示任何内容,因此我使用了以下内容,并且效果很好

// $search_text = the_excerpt();
$search_text = get_the_excerpt();

// Strip the <p> tag by replacing it empty string
$tags = array("<p>", "</p>");
$search_content = str_replace($tags, "", $search_text);

// Echo the content

echo $search_content;

我希望这也为其他人带来更多的启发。

干杯


-1

以下是使用ACF插件的技巧:

<p>
    <?php
        $summary = get_field('introductory_text');
        echo strip_tags(substr($summary, 0, 520));
    ?>
    <a href="<?php the_permalink(); ?>"> ...read more</a>
</p>
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.