get_the_excerpt()删除页面中的所有p标记

时间:2015-07-19 07:35:28

标签: wordpress wordpress-theming

我已经写了一些短代码,以便能够在页面的任何位置放置最新的帖子摘录,但我遇到了一个问题,我现在试图解决几天!我无法找到get_the_excerpt()删除页面其他部分中文本周围所有p标记的原因。当我从代码中删除get_the_excerpt()时,其他短代码中的其他文本没有问题,但是当我把我放回去时,问题又回来了!请看一下代码并告诉我我做错了什么或者可以改进!谢谢!

<?php

function show_posts() { 
    global $post;

    $html = "";
    $extra_css = "";
    $latestPosts = new WP_Query('cat=5&posts_per_page=1');

    if($latestPosts->have_posts()): 
        while($latestPosts->have_posts()): $latestPosts->the_post();

        if (has_post_thumbnail()) {     
            $extra_css = "has-thumbnail";
        }

        $html = "<div class='wide'><article class='post {$extra_css}'>";
        $html .= "<div class='post-thumbnail'>";
        $html .= "<a href='" . get_the_permalink() . "'>" . get_the_post_thumbnail(null,'thumbnail') . "</a>";  
        $html .= "<div class='post-thumbnail-date'><h4>" . get_the_time('Y-m-d') . " </h4></div>";
        $html .= "</div><!-- end post-thumbnail -->";
        $html .= "<h3><a href='" . get_the_permalink() . "'>" . get_the_title() . "</a></h3>";
        $html .= "<p class='post-info'>" . get_the_time('Y-m-d') . " | Av " . get_the_author() . "</p>";
        $html .= "<p class='post-text'>" . get_the_excerpt() . "</p><p><a href='" . get_the_permalink() . "'> Läs mer...</a></p>"; 
        $html .= "</article></div>";

        endwhile;
    else:
        // Error message
    endif;

    wp_reset_postdata();
    return $html;
}
add_shortcode('blogg','show_posts');

1 个答案:

答案 0 :(得分:0)

"avi"正在删除其他地方的get_the_excerpt()代码,因为您使用的是<p>,并且正在将wpautop嵌套在一个段落中。这意味着您最终会使用嵌套的get_the_excerpt()标记,这是无效的HTML,并导致解析器混淆。

相反,您应该将<p>嵌套在get_the_excerpt()(或其他块级元素)中:

<div>