admin管理员组文章数量:1026989
I have the following code and want to add a selected featured image
$arg = array(
'orderby' => 'date',
'number' => 10,
);
$categories = get_categories($arg);
foreach ($categories as $cat) {
echo '<li>';
echo '<figure>
<a href="?catId='.$cat->cat_ID.'">';
//Add featured image from the last post??
get_last_post_image($cat->name);
echo '</a>
</figure>';
</li>';
}
And I have this function:
function get_last_post_image($cat_name){
$args = array(
'category_name' => '$cat_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($page->ID, 'thumbnail');
}
}
}
But the featured image is never displayed.
I have the following code and want to add a selected featured image
$arg = array(
'orderby' => 'date',
'number' => 10,
);
$categories = get_categories($arg);
foreach ($categories as $cat) {
echo '<li>';
echo '<figure>
<a href="?catId='.$cat->cat_ID.'">';
//Add featured image from the last post??
get_last_post_image($cat->name);
echo '</a>
</figure>';
</li>';
}
And I have this function:
function get_last_post_image($cat_name){
$args = array(
'category_name' => '$cat_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($page->ID, 'thumbnail');
}
}
}
But the featured image is never displayed.
Share Improve this question edited Mar 25, 2019 at 16:26 supershivas asked Mar 25, 2019 at 15:30 supershivassupershivas 1035 bronze badges 2- 1 "Title Background Image" is not a field created by WordPress Core - it's being added through either a plugin or your theme. You'll want to search through them to find out what plugin or theme is adding it, and then you can use their support channels or browse through their code to find out how they are saving it and how to retrieve it. :) – WebElaine Commented Mar 25, 2019 at 15:49
- Thanks, I edited my question consequently... – supershivas Commented Mar 25, 2019 at 16:27
1 Answer
Reset to default 0Correct this line in your code at the end of function.
echo get_the_post_thumbnail($page->ID, 'thumbnail');
should be
echo get_the_post_thumbnail($post_id, 'thumbnail');
also as per Wordpress documentation, category_name
parameter should be a string representing category slug
, not name
.
So, the complete code should look like this:
$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>';
And the function:
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');
}
}
}
I have the following code and want to add a selected featured image
$arg = array(
'orderby' => 'date',
'number' => 10,
);
$categories = get_categories($arg);
foreach ($categories as $cat) {
echo '<li>';
echo '<figure>
<a href="?catId='.$cat->cat_ID.'">';
//Add featured image from the last post??
get_last_post_image($cat->name);
echo '</a>
</figure>';
</li>';
}
And I have this function:
function get_last_post_image($cat_name){
$args = array(
'category_name' => '$cat_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($page->ID, 'thumbnail');
}
}
}
But the featured image is never displayed.
I have the following code and want to add a selected featured image
$arg = array(
'orderby' => 'date',
'number' => 10,
);
$categories = get_categories($arg);
foreach ($categories as $cat) {
echo '<li>';
echo '<figure>
<a href="?catId='.$cat->cat_ID.'">';
//Add featured image from the last post??
get_last_post_image($cat->name);
echo '</a>
</figure>';
</li>';
}
And I have this function:
function get_last_post_image($cat_name){
$args = array(
'category_name' => '$cat_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($page->ID, 'thumbnail');
}
}
}
But the featured image is never displayed.
Share Improve this question edited Mar 25, 2019 at 16:26 supershivas asked Mar 25, 2019 at 15:30 supershivassupershivas 1035 bronze badges 2- 1 "Title Background Image" is not a field created by WordPress Core - it's being added through either a plugin or your theme. You'll want to search through them to find out what plugin or theme is adding it, and then you can use their support channels or browse through their code to find out how they are saving it and how to retrieve it. :) – WebElaine Commented Mar 25, 2019 at 15:49
- Thanks, I edited my question consequently... – supershivas Commented Mar 25, 2019 at 16:27
1 Answer
Reset to default 0Correct this line in your code at the end of function.
echo get_the_post_thumbnail($page->ID, 'thumbnail');
should be
echo get_the_post_thumbnail($post_id, 'thumbnail');
also as per Wordpress documentation, category_name
parameter should be a string representing category slug
, not name
.
So, the complete code should look like this:
$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>';
And the function:
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');
}
}
}
本文标签: categoriesHow to get featured image of last post in a category
版权声明:本文标题:categories - How to get featured image of last post in a category? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745659913a2161826.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论