【发布时间】:2014-05-06 15:09:02
【问题描述】:
我正在尝试使用简码在主页(静态页面)上创建我的博客提要。
到目前为止,我已经设法让它显示标题,但我还希望它在每个显示的帖子标题下显示大约 250 个字符的条目内容。
换句话说,我只需要它显示标题和前几句。
有可能吗?
这是用于创建提要的代码。(它在标签内显示预定义数量的最新帖子标题)
function getblogposts($atts, $content = null) {
extract(shortcode_atts(array(
'posts' => 1,
), $atts));
$return_string = '<h3>'.$content.'</h3>';
$return_string .= '<ul>';
query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
if (have_posts()) :
while (have_posts()) : the_post();
$return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
endwhile;
endif;
$return_string .= '</ul>';
wp_reset_query();
return $return_string;
已解决 特别感谢 pmandell 和 gtr1971。
完整的 feed.php 代码,用于创建限制为 100 个字符的提要。
<?php
function getblogposts($atts, $content = null) {
extract(shortcode_atts(array(
'posts' => 1,
), $atts));
$return_string = '<h3>'.$content.'</h3>';
$return_string .= '<ul>';
query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
if (have_posts()) :
while (have_posts()) : the_post();
$return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a>';
$return_string .= '<div class="excerpt">' . get_the_excerpt() . '</div></li>';
endwhile;
endif;
$return_string .= '</ul>';
wp_reset_query();
return $return_string;
}
function new_excerpt_more( $more ) {
return ' <a class="read-more" href="'. get_permalink( get_the_ID() ) . '">[...]</a>';
}
add_filter( 'excerpt_more', 'new_excerpt_more' );
function custom_excerpt_length( $length ) {
return 20;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 100 );
?>
【问题讨论】:
标签: php wordpress feed shortcode