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

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' );

Related

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')

Retain formatting while using excerpt [ wp_trim_words() ]

I am using <?php echo wp_trim_words(get_the_content(), 100); ?> in my template file to control amount of words to be displayed on a page and also have a link below it for taking user to the next page to read entire content but this function removes all the formatting of content on preview page.
I can't use wordpress default excerpt function here as it is being used elsewhere and i need this to be of different length than that. Is there a way to retain formatting while using this ?
Thanks
I found a solution to this may be it can help others as well.
function content_excerpt($excerpt_length = 5, $id = false, $echo = true) {
$text = '';
if($id) {
$the_post = & get_post( $my_id = $id );
$text = ($the_post->post_excerpt) ? $the_post->post_excerpt : $the_post->post_content;
} else {
global $post;
$text = ($post->post_excerpt) ? $post->post_excerpt : get_the_content('');
}
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
if ( count($words) > $excerpt_length ) {
array_pop($words);
$text = implode(' ', $words);
$text = $text . $excerpt_more;
} else {
$text = implode(' ', $words);
}
if($echo)
echo apply_filters('the_content', $text);
else
return $text;
}
function get_content_excerpt($excerpt_length = 5, $id = false, $echo = false) {
return content_excerpt($excerpt_length, $id, $echo);
}
// Call function in template file via
<?php content_excerpt(50); // 50 is amount to words ?>
To anyone else facing similar issues, I took the default wp_trim_words() function directly out of core, and altered it to not strip tags:
function wp_trim_words_retain_formatting( $text, $num_words = 55, $more = null ) {
if ( null === $more )
$more = __( '…' );
$original_text = $text;
/* translators: If your word count is based on single characters (East Asian characters),
enter 'characters'. Otherwise, enter 'words'. Do not translate into your own language. */
if ( 'characters' == _x( 'words', 'word count: words or characters?' ) && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) ) {
$text = trim( preg_replace( "/[\n\r\t ]+/", ' ', $text ), ' ' );
preg_match_all( '/./u', $text, $words_array );
$words_array = array_slice( $words_array[0], 0, $num_words + 1 );
$sep = '';
} else {
$words_array = preg_split( "/[\n\r\t ]+/", $text, $num_words + 1, PREG_SPLIT_NO_EMPTY );
$sep = ' ';
}
if ( count( $words_array ) > $num_words ) {
array_pop( $words_array );
$text = implode( $sep, $words_array );
$text = $text . $more;
} else {
$text = implode( $sep, $words_array );
}
/**
* Filter the text content after words have been trimmed.
*
* #since 3.3.0
*
* #param string $text The trimmed text.
* #param int $num_words The number of words to trim the text to. Default 5.
* #param string $more An optional string to append to the end of the trimmed text, e.g. ….
* #param string $original_text The text before it was trimmed.
*/
return apply_filters( 'wp_trim_words', $text, $num_words, $more, $original_text );
}
Try:
echo apply_filters( 'the_content', wp_trim_words( get_the_content(), 100 ) );

Need help modifying excerpt code

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>';
}

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