Last active
April 16, 2022 14:52
-
-
Save paaljoachim/2adcc2fc7eae059d5be47a4ccb77aa86 to your computer and use it in GitHub Desktop.
Featured image for Genesis themes. 1. Sets the featured image. 2. If no featured image get image from category. 3. If no category image then get the first post image. 4. If no post image or category image then sets a fallback image.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// GENESIS get featured image. | |
// 1. Sets the featured image. | |
// 2. If no featured image then get image from category. | |
// 3. If no category image then get the first post image. | |
// 4. If no post image or category image then set a fallback image. | |
// Add to your functions file. | |
// Resources | |
// https://wordpress.org/support/topic/make-first-image-in-post-featured-if-no-featured-is-set?replies=9 | |
// http://wpsites.net/web-design/add-default-featured-image-for-each-post-in-a-category/ | |
// https://codex.wordpress.org/Conditional_Tags | |
function get_src() { | |
if ( has_post_thumbnail() ) { | |
$src = wp_get_attachment_image_src( get_post_thumbnail_id(), 'thumb' ); | |
$fbimage = $src[0]; | |
} else { | |
global $post, $posts; | |
$fbimage = ''; | |
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', | |
$post->post_content, $matches); | |
$fbimage = $matches [1] [0]; | |
} | |
if(empty($fbimage) && in_category('WordPress') ) { | |
$fbimage = get_stylesheet_directory_uri().'/images/Leaves-white.jpg'; | |
} | |
if(empty($fbimage) && in_category('images') ) { | |
$fbimage = get_stylesheet_directory_uri().'/images/Tree-road-sun.jpg'; | |
} | |
if(empty($fbimage) && in_category('Uncategorized') ) { | |
$fbimage = get_stylesheet_directory_uri().'/images/front-page-1.jpg'; | |
} | |
if(empty($fbimage)) { | |
$fbimage = get_stylesheet_directory_uri().'/images/Red-back.jpg'; | |
/*$fbimage = site_url().'/wp-content/uploads/Red-back.jpg'; Works. | |
$fbimage = site_url().'/images/Red-back.jpg'; Works | |
$fbimage = CHILD_URL.'/images/Red-back.jpg'; Works - Genesis. But do not use CHILD_URL.*/ | |
} | |
return $fbimage; | |
} | |
add_filter('genesis_get_image', 'default_image_fallback', 10, 2); | |
function default_image_fallback($output, $args) { | |
return get_image(); | |
} | |
function get_image($class="") { | |
$src = get_src(); | |
ob_start()?> | |
<a href="<?php echo get_permalink() ?>"> | |
<img class="featured-image <?php echo $class ?>" src="<?php echo $src ?>" alt="<?php echo get_the_title() ?>" /> | |
</a> | |
<?php return ob_get_clean(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code feels more robust and works better then the general featured image snippet: https://gist.github.com/paaljoachim/857644243e033668101615e14d85bb69
It also has link directly to image url instead of attachment id.
What I want to do is add code to the Genesis snippet so that it can be added to a functions file for most themes not just Genesis themes.