您可以将帖子模板(通常为index.php,但取决于主题)修改为:
检查当前请求是否应响应这些帖子添加一个段,该段将查询数据库以检索您想要的帖子,如您所愿回显帖子标题和摘录。以下代码将查询数据库并返回一个包含5篇最新帖子和摘录的字符串。请注意$before
, $after
, $before_excerpt
, $after_excerpt
, 这些只是为了让内容看起来更好而对其进行包装,您可以将所有这些内容作为参数发送给函数,但为了简单起见,我对它们进行了硬编码。
function get_posts()
{
global $wpdb;
$post_count = 5;
$before = \'<h3>\'; // this will be rendered before the post content
$after = \'<br />\'; // this will be rendered after the post content
$before_excerpt = \'</h3><p>\'; // this will be rendered before the post excerpt
$after_excerpt = \'</p>\'; // this will be rendered after the post excerpt
$request =
"
select ID,
post_date,
post_title,
post_excerpt
from $wpdb->posts p inner join
$wpdb->term_relationships r on r.object_id = p.ID inner join
$wpdb->terms t on t.term_id = r.term_taxonomy_id
where post_status = \'publish\'
AND
post_password = \'\'
AND
post_type = \'post\'
GROUP BY
ID,
post_date,
post_title,
post_excerpt
ORDER BY
post_date DESC
LIMIT 0, $post_count
";
$posts = $wpdb->get_results($request);
$output = \'\';
if ($posts)
{
foreach ($posts as $post)
{
$post_title = $post->post_title;
$permalink = get_permalink($post->ID);
$output .= $before;
$output .= \'<a href="\' . esc_url($permalink) . \'" rel="bookmark" title="Permanent Link: \' . esc_attr($post_title) . \'">\' . esc_html($post_title) . \'</a>\';
$post_excerpt = esc_html($post->post_excerpt);
$output .= $before_excerpt . $post_excerpt . $after_excerpt;
$output .= $after;
}
}
return $output;
}