2015-07-19 43 views
0

我已經寫了一些簡碼以便能夠將最新的帖子摘要放置在頁面的任何位置,但是我遇到了一個問題,我現在試圖解決這些問題!而且我找不到get_the_excerpt()爲什麼會刪除頁面其他部分文本週圍的所有p標籤。當我從代碼中刪除get_the_excerpt()時,其他短文內的其他文本沒有問題,但是當我將其放回時,問題又回來了!請查看代碼並告訴我我做了什麼錯誤或可以改進!謝謝!get_the_excerpt()刪除頁面中的所有p標籤

<?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'); 

回答

0

get_the_excerpt()在別處去除<p>標籤,因爲你正在使用wpautop,並在段落中嵌套get_the_excerpt()。這意味着你最終會嵌套<p>標記,這是無效的HTML,並導致解析器感到困惑。

相反,你應該在<div>(或其他塊級元素)中嵌套get_the_excerpt()

$html .= "<div class='post-text'>" . get_the_excerpt() . "</div><p><a href='" . get_the_permalink() . "'> Läs mer...</a></p>"; 
+0

謝謝,但它並沒有幫助!我複製/粘貼你的代碼行,我想你的問題最終會被解決,但沒有:(其他想法?我的p標籤是否真的嵌套!?對我來說,它們看起來像是他們開始和結束在對方之後? –