Last active
May 24, 2019 15:24
-
-
Save w-jerome/fed14496a91cafcbac25c631c95bb7c6 to your computer and use it in GitHub Desktop.
WordPress - get post excerpt from Gutenberg
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
<?php | |
function get_excerpt($post) { | |
$excerpt = $post->post_excerpt; | |
if (empty($post->post_excerpt)) { | |
$_html = explode("\n", trim(strip_tags($post->post_content, '<p>'))); | |
$_html = array_filter($_html, function($var) { | |
return (is_int(stripos($var, '<p'))) ? true : false; | |
}); | |
$_html = array_values($_html); | |
if (!empty($_html)) { | |
$excerpt = strip_tags($_html[0]); | |
} | |
} | |
return $excerpt; | |
} |
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
<?php | |
$post = array( | |
'post_title' => 'Article 1', | |
'post_content' => '<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas ac libero felis. Praesent finibus leo non leo semper, eu viverra urna gravida. Suspendisse in euismod arcu. Duis congue tortor nec ligula viverra cursus. Sed nunc quam, ornare eu venenatis vitae, malesuada in ex. Nulla rhoncus sapien vel hendrerit ullamcorper. Morbi et ultricies ex, id facilisis ante. Curabitur nunc felis, finibus convallis porttitor a, convallis eget metus. Donec urna ligula, gravida et justo sit amet, euismod faucibus libero. Curabitur lobortis ornare magna sit amet posuere.</p><p>Aliquam mollis eget nisl vitae finibus. Maecenas faucibus felis sagittis faucibus vehicula. Donec congue magna nec fermentum aliquet. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. In eget magna sit amet risus gravida vestibulum ut et justo. In hac habitasse platea dictumst. Suspendisse luctus, sem quis suscipit ultrices, enim dui egestas neque, id tristique lectus erat feugiat elit.</p>', | |
'post_excerpt' => 'Is excerpt', | |
); | |
// Convert array to class | |
$post = json_decode(json_encode($post, JSON_UNESCAPED_SLASHES)); | |
var_dump(get_excerpt($post)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment