This tutorial shows you different ways to add sub titles with unique content with your post titles like this:
You can add the content:
- Before or after your entry titles
- To all single post titles or specific archive page like home, blog, category and other taxonomy pages
- Show the excerpt if no sub title added
- Control the length of the sub title
Here’s the code snippets for logged in members:
This code adds the sub title after the single post entry title using the content you add to the sub_title custom field value field. If no custom field value is found, the excerpt is shown as your title title as a fallback.
PHP code for functions file
add_action( 'genesis_entry_header', 'wpsites_subtitle', 11 );
function wpsites_subtitle() {
$custom_field = genesis_get_custom_field('sub_title');
$excerpt = wp_trim_words( get_the_excerpt(), 50, '...' );
if ( ! is_singular( 'post' ) )
return;
if ( ! empty( $custom_field )) :
printf( '<p class="sub-title">%s</p>', $custom_field );
else :
printf( '<p class="sub-title">%s</p>', $excerpt );
endif;
}
CSS
.sub-title {
font-style: italic;
font-size: 15px;
}
Then add your content to a custom field as seem in the following example:
Add Sub Title Before Title
This example adds your sub title before the title on the home archive page only. The code is written using ternary operators rather than if else statements.
add_action( 'genesis_entry_header', 'subtitle_before_title', 5 );
function subtitle_before_title() {
if ( ! is_home() )
return;
$custom_field = genesis_get_custom_field('sub_title');
$excerpt = wp_trim_words( get_the_excerpt(), 50, '...' );
$output = ! empty( $custom_field ) ? $custom_field : $excerpt;
printf( '<p class="sub-title">%s</p>', $output );
}
Note: Change the 50 to control the word count in the excerpt.
Add Sub Title After Entry Title Conditionally
This code enables you to show the sub title after the title using a conditional tag
add_action( 'genesis_entry_header', 'subtitle_after_title' );
function subtitle_after_title() {
if ( ! is_front_page() )
return;
$custom_field = genesis_get_custom_field('sub_title');
$output = ! empty( $custom_field ) ? $custom_field : '';
printf( '<p class="sub-title">%s</p>', $output );
}
Swap out the is_front_page
conditional tag for usage on other archives.
Leave a Reply