Community Forums › Forums › Archived Forums › General Discussion › Default Featured Imaged depending on author
- This topic has 9 replies, 5 voices, and was last updated 9 years, 11 months ago by Brad Dalton.
-
AuthorPosts
-
October 28, 2014 at 6:00 pm #129650SelenaDMember
I need some PHP help - it's not my strong suit. I'm hoping one of you geniuses can help me out!
I can successfully use this snippet to add a default image:
add_filter('genesis_get_image', 'default_image_fallback', 10, 2); function default_image_fallback() { if(has_post_thumbnail()) { the_post_thumbnail(); } else echo '<img src="' . trailingslashit( get_stylesheet_directory_uri() ) . 'images/sam-thumb.jpg' . '" alt="" />'; }
However, I need to have my default image be different depending on who the author is. So I tried this:
add_filter('genesis_get_image', 'default_image_fallback', 10, 2); function default_image_fallback() { $author_id=$post->post_author; if(has_post_thumbnail()) { the_post_thumbnail(); } elseif($author_id = "2") { echo '<img src="' . trailingslashit( get_stylesheet_directory_uri() ) . 'images/jon-thumb.jpg' . '" alt="Jonathan Warner" />'; } elseif($author_id = "3") { echo '<img src="' . trailingslashit( get_stylesheet_directory_uri() ) . 'images/sam-thumb.jpg' . '" alt="Samuel Warner" />'; } }
However, I get the image for $author_id="2" for both authors. What am I missing?
Thanks to anyone who can help!
http://67.225.230.212/~warnerlawyers/blog/October 29, 2014 at 3:21 am #129684Sridhar KatakamParticipantTry changing the last elseif to else.
It should be like this:
if ( condition ) { action1(); action2(); } elseif ( condition2 && condition3 ) { action3(); action4(); } else { defaultaction(); }
Source: http://make.wordpress.org/core/handbook/coding-standards/php/
October 29, 2014 at 7:12 am #129716SelenaDMemberI still only get the the picture of author id = 2.
I tried this:
$author_id=the_author_meta('ID'); function default_image_fallback() { if(has_post_thumbnail()) { the_post_thumbnail(); } elseif($author_id = "2") { echo '<img src="' . trailingslashit( get_stylesheet_directory_uri() ) . 'images/jon-thumb.jpg' . '" alt="Jonathan Warner" />'; } elseif($author_id = "3") { echo '<img src="' . trailingslashit( get_stylesheet_directory_uri() ) . 'images/sam-thumb.jpg' . '" alt="Samuel Warner" />'; } else { echo '<img src="' . trailingslashit( get_stylesheet_directory_uri() ) . 'images/default-thumb.jpg' . '" alt="Samuel and Jonathan Warner" />'; } }
I also tried removing the second "elseif" and moving straight to the else statement. That didn't work either - still got the first author's picture each time.
I tried defining the author id differently just in case that was the problem, but it isn't. I've echoed the $author_id to make sure it's pulling the correct id for each author and it is. 🙁
October 29, 2014 at 11:28 am #129770Brad DaltonParticipantOctober 30, 2014 at 10:15 am #129894SelenaDMemberThank you Brad - that did the trick!
November 9, 2014 at 9:35 am #130984decarnMemberHi,
Can someone help on how I can change the fallback image from authors to categories?
Thanks
November 9, 2014 at 11:26 am #130992Brad DaltonParticipantNovember 9, 2014 at 12:16 pm #130996decarnMemberHi Brad,
I notice two sets of code. May I know the difference and which is more efficient?
February 26, 2015 at 5:41 pm #142447joycegraceParticipantThat code snippet doesn't work for me. I'm baffled as to why. Does anyone know how I can just add a default featured image? Doesn't have to depend on the author or category.
Find me at Joyce Grace (http://www.joycegrace.ca)
February 26, 2015 at 8:38 pm #142464Brad DaltonParticipantThere is another snippet there which only displays a default image for all posts that don't have one rather than for each category.
-
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.