Skip to content

Instantly share code, notes, and snippets.

@gaelbillon
Last active September 27, 2021 13:31
Show Gist options
  • Save gaelbillon/bf6d3754b31da58faa1f233df298297d to your computer and use it in GitHub Desktop.
Save gaelbillon/bf6d3754b31da58faa1f233df298297d to your computer and use it in GitHub Desktop.
Shortcode to display the post content if it is not empty, otherwise it displays a fallback text.
// Shortcode : [post-content-with-fallback-text-if-empty]
function empty_content($str) {
return trim(str_replace(' ','',strip_tags($str))) == '';
}
function my_sc_content() {
$post = get_post( $id );
$content = $post->post_content;
if ( ! $post ) {
return '';
}
if (empty_content($content)) {
return "La description de ce projet n'est pas encore renseignée.";
} else {
return $content;
}
}
add_shortcode('post-content-with-fallback-text-if-empty','my_sc_content');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment