Skip to content

Instantly share code, notes, and snippets.

@mwordpress
Created February 9, 2017 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mwordpress/5e977c08fb78acd7927a8af2d47031f0 to your computer and use it in GitHub Desktop.
Save mwordpress/5e977c08fb78acd7927a8af2d47031f0 to your computer and use it in GitHub Desktop.
the codes listed here is part of the following article : https://www.mwordpress.net/how-to-add-read-more-for-wordpress-post-in-home-page/
/*
* get content and clean
*/
function mwp_post_content($post_id) {
$post = get_post($post_id);
$output = $post->post_content;
$replace = "@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@";
$output = wp_strip_all_tags($output, true);
$output = strip_shortcodes($output);
$output = ltrim($output);
$output = trim($output);
$output = preg_replace($replace, '', $output);
return $output;
}
/*
* Content Word Limit
*/
function content($limit, $post_id) {
$data = mwp_post_content($post_id);
$content = explode(' ', $data, $limit);
if (count($content) >= $limit) {
array_pop($content);
$content = implode(" ",$content).'…';
} else {
$content = implode(" ",$content);
}
return $content;
}
<div class="textPreview">
<?php echo content(30, $post->ID); ?>
</div>
<div class="textPreview">
<?php the_content('More …'); ?>
</div>
<div class="textPreview">
<?php the_excerpt(); ?>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment