在函数末尾更正代码中的这一行。
echo get_the_post_thumbnail($page->ID, \'thumbnail\');
应该是
echo get_the_post_thumbnail($post_id, \'thumbnail\');
也按照Wordpress
documentation,
category_name
参数应为表示类别的字符串
slug
, 不
name
.
因此,完整的代码应该如下所示:
$arg = array(
\'orderby\' => \'date\',
\'number\' => 10,
);
$categories = get_categories($arg);
echo \'<ul>\';
foreach ($categories as $cat) {
echo \'<li>\';
echo \'<figure>
<a href="?catId=\'.$cat->term_id.\'">\'; // not cat_ID
//Add featured image from the last post??
get_last_post_image($cat->slug);
echo \'</a>
</figure>
</li>\';
}
echo \'</ul>\';
以及功能:
function get_last_post_image($cat_slug){
$args = array(
\'category_name\' => $cat_slug, // it should be slug of category, not name.
\'posts_per_page\' => 1,
\'order_by\' => \'date\',
\'order\' => \'desc\'
);
$post = get_posts( $args );
if($post) {
$post_id = $post[0]->ID;
if(has_post_thumbnail($post_id)){
echo get_the_post_thumbnail($post_id, \'thumbnail\');
}
}
}