我想在页面中显示类别中的最新帖子内容。
例如,类别foo
拥有以下职位:
你好,世界你好,考虑到Foo bar是Foo类别的最新文章,其内容应呈现在一个页面中:
<title>
<content>
在哪里<title>
是Foo bar和<content>
是帖子内容。我该怎么做?
我正在努力实现@Pieter\'s答案。我在中添加了这些行functions.php
:
function latest_post() {
$args = array(
\'posts_per_page\' => 1, // we need only the latest post, so get that post only
\'cat\' => \'4\' // Use the category id, can also replace with category_name which uses category slug
);
$str = "";
$posts = get_posts($args);
foreach($posts as $post):
$str = $str."<h2>".$post->title."</h2>";
$str = $str."<p class=\'post-content-custom\'>".$post->content."</p>";
endforeach;
return $str;
}
add_shortcode(\'latest_post\', \'latest_post\');
在页面中,我会:[latest_post]
但是,不会出现错误,但不会显示帖子内容。