Created
February 9, 2017 20:52
-
-
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/
This file contains 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
/* | |
* 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; | |
} |
This file contains 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
<div class="textPreview"> | |
<?php echo content(30, $post->ID); ?> | |
</div> |
This file contains 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
<div class="textPreview"> | |
<?php the_content('More …'); ?> | |
</div> |
This file contains 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
<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