Last active
October 29, 2016 01:09
-
-
Save srikat/8b899a8d59f719286fd0 to your computer and use it in GitHub Desktop.
How to truncate alt text for featured images on content archives in Genesis. https://sridharkatakam.com/how-to-truncate-alt-text-for-featured-images-on-content-archives-in-genesis/
This file contains 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
// Function to truncate post titles | |
function customTitle( $limit ) { | |
$title = get_the_title( $post->ID ); | |
if ( strlen( $title ) > $limit ) { | |
$title = substr( $title, 0, $limit ) . '...'; | |
} | |
return $title; | |
} | |
// genesis_entry_header action hook is used here because it is the one immediately above genesis_entry_content. Remember: Hook as late as possible. | |
add_action( 'genesis_entry_header', 'sk_custom_post_image' ); | |
function sk_custom_post_image() { | |
// if we are on a content archive page and featured images are set to be shown on content archives in Genesis theme settings | |
if ( ! is_singular() && genesis_get_option( 'content_archive_thumbnail' ) ) { | |
// remove the standard featured image output by Genesis per theme settings | |
remove_action( 'genesis_entry_content', 'genesis_do_post_image', 8 ); | |
// add custom featured image output | |
add_action( 'genesis_entry_content', 'sk_do_post_image', 8 ); | |
} | |
} | |
// Function to set the alt text of featured image to truncated post title text | |
function sk_do_post_image() { | |
$img = genesis_get_image( array( | |
'format' => 'html', | |
'size' => genesis_get_option( 'image_size' ), | |
'context' => 'archive', | |
'attr' => genesis_parse_attr( 'entry-image', array ( 'alt' => customTitle( 20 ) ) ), | |
) ); | |
if ( ! empty( $img ) ) { | |
printf( '<a href="%s" aria-hidden="true">%s</a>', get_permalink(), $img ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment