admin管理员组文章数量:1122846
Can I do this for posts?
I have the most recents posts I am fetching and I am showing the thumbnail (the featured image) of these posts IF they exist.
What I want to do now, is to show category images IF the featured image does not exist.
Can someone help me with this?
I have this, at the moment:
<?php
$args = array( 'numberposts' => '5' );
$recent_posts = wp_get_recent_posts($args);
$category = get_the_category();
foreach( $recent_posts as $recent ){
if($recent['post_status']=="publish"){
if ( has_post_thumbnail($recent["ID"])) {
echo '<a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . get_the_post_thumbnail($recent["ID"], 'thumbnail').'<div class="browse_category_name"> ' . $recent["post_title"]. '<div> ' . get_the_author_meta('display_name', $recent["post_author"]). '</div></div></a></li> ';
} else{
echo '<a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . get_categories_with_images($post->ID,' ,') . $recent["post_title"].'</a></li> ';
}
}
}
?>
And this is my functions.php:
function get_categories_with_images($post_id,$separator ){
//first get all categories of that post
$post_categories = wp_get_post_categories( $post_id );
$cats = array();
foreach($post_categories as $c){
$cat = get_category( $c );
$cat_data = get_option("category_$c");
//and then i just display my category image if it exists
$cat_image = '';
if (isset($cat_data['img'])){
$cat_image = '<img src="'.$cat_data['img'].'">';
}
$cats[] = $cat_image . '<a href="'.get_category_link( $c ) . '">' .$cat->name .'</a>';
}
return implode($separator , $cats);
}
Problem: category image is not shown, even though thumbnail / featured image is not set.
Can I do this for posts?
I have the most recents posts I am fetching and I am showing the thumbnail (the featured image) of these posts IF they exist.
What I want to do now, is to show category images IF the featured image does not exist.
Can someone help me with this?
I have this, at the moment:
<?php
$args = array( 'numberposts' => '5' );
$recent_posts = wp_get_recent_posts($args);
$category = get_the_category();
foreach( $recent_posts as $recent ){
if($recent['post_status']=="publish"){
if ( has_post_thumbnail($recent["ID"])) {
echo '<a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . get_the_post_thumbnail($recent["ID"], 'thumbnail').'<div class="browse_category_name"> ' . $recent["post_title"]. '<div> ' . get_the_author_meta('display_name', $recent["post_author"]). '</div></div></a></li> ';
} else{
echo '<a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . get_categories_with_images($post->ID,' ,') . $recent["post_title"].'</a></li> ';
}
}
}
?>
And this is my functions.php:
function get_categories_with_images($post_id,$separator ){
//first get all categories of that post
$post_categories = wp_get_post_categories( $post_id );
$cats = array();
foreach($post_categories as $c){
$cat = get_category( $c );
$cat_data = get_option("category_$c");
//and then i just display my category image if it exists
$cat_image = '';
if (isset($cat_data['img'])){
$cat_image = '<img src="'.$cat_data['img'].'">';
}
$cats[] = $cat_image . '<a href="'.get_category_link( $c ) . '">' .$cat->name .'</a>';
}
return implode($separator , $cats);
}
Problem: category image is not shown, even though thumbnail / featured image is not set.
Share Improve this question edited Sep 19, 2017 at 9:32 Siyah asked Sep 19, 2017 at 9:26 SiyahSiyah 1932 silver badges12 bronze badges2 Answers
Reset to default 0You have a typo. In your loop you're checking has_post_thumbnail($recent["ID"])
, but $recent['ID']
won't be set because the variable for each post in your foreach
is $decent
:
foreach( $recent as $decent ){
Change your code to :
<?php
$args = array( 'numberposts' => '5' );
$recent = wp_get_recent_posts($args);
$category = get_the_category();
foreach( $recent as $decent ){
if($decent['post_status']=="publish"){
if ( has_post_thumbnail($decent["ID"])) {
echo '<a href="' . get_permalink($decent["ID"]) . '" title="Look '.esc_attr($decent["post_title"]).'" >' . get_the_post_thumbnail($decent["ID"], 'thumbnail').'<div class="browse_category_name"> ' . $decent["post_title"]. '<div> ' . get_the_author_meta('display_name', $decent["post_author"]). '</div></div></a></li> ';
} else{
echo '<a href="' . get_permalink($decent["ID"]) . '" title="Look '.esc_attr($decent["post_title"]).'" >' . get_categories_with_images($post->ID,' ,') . $decent["post_title"].'</a></li> ';
}
}
}
?>
The post is now $decent, not $recent
本文标签: postsHow to show category image if no featured image is set
版权声明:本文标题:posts - How to show category image if no featured image is set? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736301049a1931042.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论