Need help modifying excerpt code - php

My wordpress theme currently adds "..." to the end of the excerpt. To read the whole post you either have to click on the featured image or on the post title.
I would like to replace the "..." with "...read more" and have it like to the post.
My blog is located # www.cur-mudg-eon.com if you need to see how it's set up right now.
The code from the theme-function.php is located in full on pastebin.
Here's the bit of code that I believe needs changing:
<?php
// The excerpt based on words
function my_string_limit_words($string, $word_limit)
{
$words = explode(' ', $string, ($word_limit + 1));
if(count($words) > $word_limit)
array_pop($words);
return implode(' ', $words).'...';
}
// The excerpt based on character
function my_string_limit_char($excerpt, $substr=0)
{
$string = strip_tags(str_replace('...', '...', $excerpt));
if ($substr>0) {
$string = substr($string, 0, $substr);
}
return $string;
}
If more information/code is needed to answer my question let me know.
Thanks

From the codex :
function new_excerpt_more( $more ) {
return ' <a class="read-more" href="'. get_permalink( get_the_ID() ) . '">...Read More</a>';
}
add_filter( 'excerpt_more', 'new_excerpt_more' );

Here is the section of code I changed:
// The excerpt based on words
function my_string_limit_words($string, $word_limit)
{
$words = explode(' ', $string, ($word_limit + 1));
if(count($words) > $word_limit)
array_pop($words);
return implode(' ', $words).'...';
}
I simply replaced the "..." with the following code:
<a class="read-more" href="'. get_permalink( get_the_ID() ) . '">...Read More</a>
This is what the final code looked like:
// The excerpt based on words
function my_string_limit_words($string, $word_limit)
{
$words = explode(' ', $string, ($word_limit + 1));
if(count($words) > $word_limit)
array_pop($words);
return implode(' ', $words).'<a class="read-more" href="'. get_permalink( get_the_ID() ) . '">...Read More</a>';
}

Related

How would one limit the Manual Excerpt Length in WP?

One can control the WP default (Automatic) excerpt length of a WP post using the using the following snippet within functions.php;
From the WP Codex
// . Post excerpt adjustment (Auto)
// . ==============================
function wpdocs_custom_excerpt_length( $length ) {
return 20;
}
add_filter( 'excerpt_length', 'wpdocs_custom_excerpt_length', 999 );
My question is how do you limit the manual one?
You know, the exerpt specifically added by the user themselves?
(*) There is an 8 year old question here, that does provide some context but given the current year and progress WP has made I want to post the question again and receive some clarity on the subject.
Added Context: (Edited: 12 March 2019)
It's not that the original answer to the question posted earlier doesn't work, all be it seems, really clunky. I'm looking for a more simple & robust answer using exerpt_length filter. Rather than using something like the following to trim the text; (If Possible)
function excerpt($limit) {
return wp_trim_words(get_the_excerpt(), $limit);
}
We have by default in core, the following filtering:
add_filter( 'get_the_excerpt', 'wp_trim_excerpt' );
but within wp_trim_excerpt() the trimming is only applied on the post's content, when there's no manual excerpt set.
Here's an untested suggestion for a custom filtering:
add_filter( 'get_the_excerpt', function( $excerpt, $post ) {
if ( has_excerpt( $post ) ) {
$excerpt_length = apply_filters( 'excerpt_length', 55 );
$excerpt_more = apply_filters( 'excerpt_more', ' ' . '[…]' );
$excerpt = wp_trim_words( $excerpt, $excerpt_length, $excerpt_more );
}
return $excerpt;
}, 10, 2 );
to apply the similar trimming on manual excerpts.
Hope you can adjust this further to your needs.
Try this, I got this code from here: https://www.wpexplorer.com/wordpress-excerpt-length/
add_filter( 'excerpt_length', function($length) {
return 20;
} );
This pair of functions will give you control over the excerpt length, including the manual excerpt which is returned if available, otherwise the "excerpt-ized" post_content gets returned. These go in your theme functions file:
function get_excerpt_by_id($post_id, $length = NULL) {
$length = isset($length) ? $length : apply_filters('excerpt_length', 32);
$p = get_post($post_id);
return $p->post_excerpt ? build_excerpt_by_length($p->post_excerpt, $length) : build_excerpt_by_length($p->post_content, $length);
}
function build_excerpt_by_length($content, $length = 32) {
$excerpt = strip_tags(strip_shortcodes($content));
$words = explode(' ', $excerpt, $length + 1);
$words = array_slice($words, 0, $length);
$result = trim(implode(' ', $words));
$result = preg_replace('/\W*$/', '', $result);
$more = apply_filters('excerpt_more', '…');
if ($result !== '') $result = $content === $result ? $result : $result . $more;
return $result;
}
Then in your templates you can use by calling:
get_excerpt_by_id($your_post_id, $preferred_excerpt_length);
You can try the following code
$excerpt = get_the_excerpt();
$excerpt = substr( $excerpt, 0, 180 );
$excerpt_description = substr( $excerpt, 0, strrpos( $excerpt, ' ' ) );
echo $excerpt_description;

Display 1st paragraph within Advanced Custom Fields

How do you display only the first paragraph of an advanced custom field.
<?php the_field('lyrics'); ?>
Above is what i use to display the full text.
add_filter( 'wp_trim_excerpt', 'my_custom_excerpt', 10, 2 );
function my_custom_excerpt($text, $raw_excerpt) {
if( ! $raw_excerpt ) {
$content = apply_filters( 'the_content', get_the_content() );
$text = substr( $content, 0, strpos( $content, '</p>' ) + 4 );
}
return $text;
}
try this code will help you to show first 55 character of your first paragraph.
Grab the first paragraph of each post
second option:
function custom_field_excerpt() {
global $post;
$text = get_field('news');
if ( '' != $text ) {
$start = strpos($text, '<p>'); // Locate the first paragraph tag
$end = strpos($text, '</p>', $start); // Locate the first paragraph closing tag
$text = substr($text, $start, $end-$start+4); // Trim off everything after the closing paragraph tag
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
}
return $text;}
third option :
You can use this function:
function get_first_paragraph(){
global $post;
$str = wpautop( get_the_content() );
$str = substr( $str, 0, strpos( $str, '</p>' ) + 4 );
$str = strip_tags($str, '<a><strong><em>');
return '<p>' . $str . '</p>';}
and then use in it in your loop with:
<?php echo get_first_paragraph(); ?>

Display WordPress full content instead post excerpt

I want to display full content instead the post excerpt. I didn't want to modify the theme. I just want to make a plugin where I will put the file.
I found something like this
function narga_excerpts($content = false) {
# If is the home page, an archive, or search results
if(is_front_page() || is_archive() || is_search()) :
global $post;
$content = $post->post_excerpt;
$content = strip_shortcodes($content);
$content = str_replace(']]>', ']]>', $content);
$content = strip_tags($content);
# If an excerpt is set in the Optional Excerpt box
if($content) :
$content = apply_filters('the_excerpt', $content);
# If no excerpt is set
else :
$content = $post->post_content;
$excerpt_length = 50;
$words = explode(' ', $content, $excerpt_length + 1);
if(count($words) > $excerpt_length) :
array_pop($words);
array_push($words, '...<p><a class="more-link" href="' . get_permalink() . '" title="' . the_title_attribute('echo=0') . '"> ' . __( 'Read more »', 'narga' ) . ' </a></p>');
$content = implode(' ', $words);
endif;
$content = '<p>' . $content . '</p>';
endif;
endif;
return $content;
}
add_filter('the_content', 'narga_excerpts')
But it's totally different. The code modify the_content to show the_excerpts. How can I change the code to display WordPress full content instead post excerpt.
Using the_content() will display the full content of a post.
Changing the function to this should work
function narga_excerpts($content = false)
{
# If is the home page, an archive, or search results
if(is_front_page() || is_archive() || is_search()) :
global $post;
$content = $post->post_content;
endif;
return $content;
}
add_filter('the_content', 'narga_excerpts')

Multiple "More" links for the_excerpt(); in Wordpress

I've changed my excerpt link in wordpress from "[...]" to "Read More" with the code below. I want to know if there is a way to use more than one link for the excerpt. Let's say for posts in one category have "Read More" for their excerpt, and then posts in another category have "See Photos" for their excerpt link. is this possible?
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'custom_trim_excerpt');
function custom_trim_excerpt($text) { // Fakes an excerpt if needed
global $post;
if ( '' == $text ) {
$text = get_the_content('');
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$text = strip_tags($text);
$excerpt_length = 24;
$words = explode(' ', $text, $excerpt_length + 1);
if (count($words) > $excerpt_length) {
array_pop($words);
array_push($words, '...<br />Read More');
$text = implode(' ', $words);
}
}
return $text;
}
yes you can do it. use excerpt_more filter to change the "[...]" and in_category function to check the category for read more text.
try this code.
function custom_excerpt_more( $more ) {
$read_more_txt = 'Read More..';
if (in_category('cat_slug'))
$read_more_txt = 'See Photos..';
else if (in_category('cat_slug2'))
$read_more_txt = 'Something else..';
return ' <a title="'. $read_more_txt .'" href="'. get_permalink( get_the_ID() ) .'">'. $read_more_txt .'</a>';
}
add_filter( 'excerpt_more', 'custom_excerpt_more' );

Custom function to limit excerpt length is returning post title as part of excerpt (WP)

I added the following to functions.php:
function excerpt($limit) {
global $id;
$excerpt = explode(' ', get_the_excerpt(), $limit);
if (count($excerpt)>=$limit) {
array_pop($excerpt);
$excerpt = implode(" ",$excerpt).'... <p class="readmore">Read More »</p>';
} else {
$excerpt = implode(" ",$excerpt).'... <p class="readmore">Read More »</p>';
}
$excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
return $excerpt;
}
and for some reason it's returning the post title as part of the excerpt (Post title comes first, then excerpt without a space between title & excerpt -- sample here: http://autisticadvocacy.uniongraphics.org/category/news/). Can anybody tell me what I am doing wrong?
$excerpt = implode(" ",$excerpt)
. '... <p class="readmore"><a href="'
. get_permalink($id)
. '" title="'
. the_title_attribute(array(
'echo' => 0,
'before' => 'Permalink to: ',
'after' => ''))
### change is in this line. (the_title() removed)
. '">Read More »</a></p>'
;
The way you write the code it's very hard to read and therefore hard to debug when you run into problems. Try to improve that. A useful function for better formatted strings is sprintf():
$excerpt = sprintf('%s ... <p class="readmode">Read More »</p>'
, implode(" ",$excerpt), get_permalink($id)
, the_title_attribute(array('echo' => 0, 'before' => 'Permalink to: ', 'after' => ''))
);
Add the following code to your functions.php file for excerpt and content.
<?php
// Custom Excerpt
function excerpt($limit) {
$excerpt = explode(' ', get_the_excerpt(), $limit);
if (count($excerpt)>=$limit) {
array_pop($excerpt);
$excerpt = implode(" ",$excerpt).'...';
} else {
$excerpt = implode(" ",$excerpt);
}
$excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
return $excerpt;
}
// Content Limit
function content($limit) {
$content = explode(' ', get_the_content(), $limit);
if (count($content)>=$limit) {
array_pop($content);
$content = implode(" ",$content).'...';
} else {
$content = implode(" ",$content);
}
$content = preg_replace('/\[.+\]/','', $content);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
return $content;
}
?>
Now, instead of using the_content() or the_excerpt in your loop, use excerpt($limit) or content($limit).
If you want to limit your excerpt to 300 words the code would look like this:
<?php echo excerpt(50); ?>
<?php echo content(300); ?>
I got another way to display limited excerpt by character. Here is the functions.php file code.
function get_excerpt(){
$excerpt = get_the_content();
$excerpt = preg_replace(" (\[.*?\])",'',$excerpt);
$excerpt = strip_shortcodes($excerpt);
$excerpt = strip_tags($excerpt);
$excerpt = substr($excerpt, 0, 100);
$excerpt = substr($excerpt, 0, strripos($excerpt, " "));
$excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt));
$excerpt = $excerpt.'... Read More';
return $excerpt;
}
After this you need to add where you want to display your customized character by character.
<?php echo get_excerpt(); ?>
Source: http://www.e2soft.com/blog/custom-excerpt-and-content-limit-wordpress/

Categories