-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>";    
?>

正如您在代码中看到的那样,例程是显示 id 为 266 的帖子,现在我只想限制该帖子的帖子内容中的字数,假设我想将字数限制为300 然后添加阅读更多链接。请问怎么做?

希望这里有人知道如何做到这一点。

我是开放的,想法,建议和建议。希望这里有人可以帮忙,谢谢。

4

1 回答 1

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 元素,则存在问题。希望能帮助到你

于 2012-10-14T22:41:05.367 回答