You could use conditional logic…
if ( function_exists('get_the_subtitle') && get_the_subtitle() ) {
the_subtitle();
} else {
the_title();
}
Does not work. I tried to broadcast the subtitle to another plugin, but if the field is not filled in, it doesn’t show the title instead. How can I put the default value from title in the subtitle field itself when creating a product or article?
I don’t know exactly what you mean by broadcasting the subtitle to another plugin, but if you can share the subtitle you should be able to wrap it in conditional logic to check if it exists first.
You might also be able to run something on save_post
to save a value if none exists. I don’t really like this as it saves a bunch of default texts to the database, but it might do the trick, and is the best I can do.
/**
* Save a default Subtitle if none exists
*
* @param int $post_id the ID of the post we're saving
* @return int
*/
function kia_save_default_subtitle( $post_id ) {
$subtitle = get_post_meta( $post_id, 'kia_subtitle', true );
if ( $ subtitle ) {
$default = esc_html__( 'A default subtitle', 'your-textdomain' );
update_post_meta( $post_id, 'kia_subtitle', $default );
}
}
add_action( 'save_post', 'kia_save_default_subtitle', 20 );
`
Thank you, I only need this for groceries. I’ll try this option.
Can you tell me how to do this only for products? Is there a filter for this, or do I need to disable all the other subtitle in the plugin settings?
In theory, I think you could save on save_post_product
… see: https://developer.wordpress.org/reference/hooks/save_post_post-post_type/
/**
* Save a default Subtitle if none exists
*
* @param int $post_id the ID of the post we're saving
* @return int
*/
function kia_save_default_subtitle( $post_id ) {
$subtitle = get_post_meta( $post_id, 'kia_subtitle', true );
if ( $ subtitle ) {
$default = esc_html__( 'A default subtitle', 'your-textdomain' );
update_post_meta( $post_id, 'kia_subtitle', $default );
}
}
add_action( 'save_post_product', 'kia_save_default_subtitle', 20 );
`
I tried, but it doesn’t work
Sorry, that’s the best I can offer. Though I did just notice that
if ( $ subtitle ) {
should be
if ( $subtitle ) {
Don’t know if that resolves your issue though. Alternatively, you could write additional conditional logic. With the post ID you can get_post_type( $post_id )
and maybe use something like that.