获取帖子并显示50个首字符


Wordpress: get posts and show 50 first characters

我想用前50个字符显示最新的三篇文章。最后还有一个"阅读更多"的链接。这样的:

我的第一个帖子

Lorem ipsum dolor sit met, consectetereadipiscing elit。Aenean vulputate。阅读更多…

我的第二个帖子

Lorem ipsum dolor sit met, consecteteadipiscing elit。Aenean vulputate。阅读更多…

我的第三篇文章

Lorem ipsum dolor sit met, consectereadipiscing elit。Aenean vulputate。阅读更多…

我使用get_posts吗?我该怎么做呢?

这就是我的解决方案:

模板中:

<?php
$args = array( 'numberposts' => 3 );
$lastposts = get_posts( $args );
foreach($lastposts as $post) : setup_postdata($post); 
?> 
<h2 class="news"><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<?php endforeach; ?>
显然也

:

function new_excerpt_more($more) {
       global $post;
    return '... <a href="'. get_permalink($post->ID) . '">Read more</a>';
}
add_filter('excerpt_more', 'new_excerpt_more');

function custom_excerpt_length( $length ) {
    return 20;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

无需进入functions.php页面即可使用此功能。

<?php
$args = array( 'numberposts' => 1 );
$lastposts = get_posts( $args );
foreach($lastposts as $post) : setup_postdata($post); ?>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_content_rss('', TRUE, '', 33); ?> <a class="continue" href="<?php     the_permalink() ?>">Continue Reading &raquo;</a>
<?php endforeach; ?>

使用get_posts:

$options = array(
    'number_of_posts' => 3
);
$myposts = get_posts($options);
foreach( $myposts as $post ) {
    setup_postdata($post);
    echo '<h2>' . the_title . '</h2>';
    echo the_content();
}

你可以使用像"$mycontent = get_the_content()"这样的东西,然后用phps的子字符串来操作它,但老实说:不要这样做!

对于你的read more函数,Wordpress在编辑器中有一个非常好的more标签,它会自动为你工作,如果你尝试这样做:

foreach( $myposts as $post ) {
    setup_postdata($post);
    echo '<h2>' . the_title . '</h2>';
    echo the_excerpt();
    echo '<a href="' . the_permalink() . '">More &raquo;</a>';
}