Skip to content

Instantly share code, notes, and snippets.

@wordpressvn
Forked from vielhuber/index.php
Created October 27, 2022 08:38
Show Gist options
  • Select an option

  • Save wordpressvn/a9498d7fedb7289882d7a2c27adf4853 to your computer and use it in GitHub Desktop.

Select an option

Save wordpressvn/a9498d7fedb7289882d7a2c27adf4853 to your computer and use it in GitHub Desktop.
split get content before / after read more tag outside loop #php #wordpress
<?php
// with wordpress native functions
echo get_extended($p->post_content)['main'];
echo get_extended($p->post_content)['extended'];
// outside of wordpress with own function
function wp_split_content($content) {
// split up
if ( preg_match('/<!--more(.*?)?-->/', $content, $matches) ) {
list($before, $after) = explode($matches[0], $content, 2);
$more_text = $matches[1];
} else {
$before = $content;
$after = '';
$more_text = '';
}
// remove whitespace
$before = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $before);
$after = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $after);
$more_text = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $more_text);
return [
'before' => $before,
'after' => $after,
'more_text' => $more_text
];
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment