php
  • wordpress
  • 2012-10-14 35 views -1 likes 
    -1

    好吧,我有這個代碼到目前爲止,這是在我的wordpress模板,所以這是一個wordpress的東西。限制字數和增加一個閱讀更多鏈接

    <?php 
    
        $post_id = 266; 
        echo "<div id='widgets-wrapper3'><div id='marginwidgets' style='overflow: auto; max-width: 100%; margin: 0 auto; border: none !important;'>"; 
    
        $queried_post = get_post($post_id); 
        echo "<div class='thewidgets'>"; 
        echo $queried_post->post_content; 
        echo '</div>'; 
    
        echo "</div></div>";  
    ?> 
    

    ,你可以看到到代碼,程序,以顯示其中有266的ID,現在我要的是限制在該職位的帖子內容的字數後,讓說我想限制單詞爲300,然後添加一個閱讀更多鏈接。如何做到這一點?

    希望有人在這裏想出瞭如何做到這一點。

    我打開,想法,建議和建議。希望這裏有人能幫忙,謝謝。

    +1

    [你有什麼嘗試?](http://whathaveyoutried.com/) –

    +1

    這個功能實際上是內置到Wordpress中。 http://codex.wordpress.org/Customizing_the_Read_More –

    +0

    @RickCalder:是的,我知道,但我只是不知道如何將它集成到我當前的代碼中,或者將該函數添加到我當前的代碼中 –

    回答

    0

    試試這個: http://codex.wordpress.org/Function_Reference/the_excerpt

    或使用PHP SUBSTR:

    echo get_sub($queried_post->post_content, 300); 
    
    
    function get_sub($str, $max=300) 
    { 
    $ar = explode($str); 
    $count = 0; 
    $new_str = ""; 
    $del = " "; 
    foreach($ar as $a) 
    { 
        if($count == 0) 
        { 
         //no space 
         $del = ""; 
        } 
    
        if($count < $max) 
        { 
         $new_str .= $del.$a; 
        } 
        $count++; 
    } 
    return $new_str; 
    } 
    

    如果內容包含HTML元素,它的一個問題。 希望它有幫助

    +0

    300個字符不等於300個字。 –

    +0

    @Kolink:所以我應該這樣做,我只會限制由一個類或一個類的p標記包裝的內容? –

    +0

    這項工作,但如果我有一個圖像到帖子中,它包含在限制 –

    相關問題