Why this function is shortening also my Archive Description? - php

I don't understand why function below influence woocommerce_taxonomy_archive_description()
Where is connection between those two? Shouldn't this work only for woocommerce_short_description?
add_filter( 'woocommerce_short_description', 'limit_woocommerce_short_description' );
function limit_woocommerce_short_description( $post_post_excerpt ) {
if(!is_product()) { // add in conditionals
$text = $post_post_excerpt;
$words = 24; // change word length
$more = ' […]'; // add a more cta
$post_post_excerpt = wp_trim_words( $text, $words, $more );
}
return $post_post_excerpt;
}

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;

Get first paragraph in wordpress

I have a problem with getting the first form the_excerpt();
Function acutally works but only for first post. I added in functions.php
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>';
}
I'm calling this funcion in index.php inside The Loop <?php echo get_first_paragraph(); ?>
I have no idea why it pulls only for first post...
you can put this code in function.php file in your theme,
Get first paragraph
function awesome_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;
}
add_filter( 'wp_trim_excerpt', 'awesome_excerpt', 10, 2 );
For more information, you can follow the reference link WORDPRESS THE_EXCERPT SHOW ONLY FIRST PARAGRAPH
That code didn't work for me in Gutenberg times. So I've used part of that code and did some searching and come up with this solution. Hope it helps.
function get_paragraph_content($paragraph_number){
global $post;
$i = 0;
$paragraph = '';
if ( has_blocks( $post->post_content ) ) {
$blocks = parse_blocks( $post->post_content );
foreach( $blocks as $block ) {
if( 'core/paragraph' === $block['blockName']){
$paragraph = render_block($block);
if (++$i == $paragraph_number) break;
}
}
$paragraph = substr( $paragraph, 0, strpos( $paragraph, '</p>' ) + 4 );
$paragraph = strip_tags($paragraph, '<a><strong><em>');
}
return $paragraph;
}

WordPress excerpt to show 2 paragraphs

I have created two categories within a WordPress site with one post in each category. I am pulling in and excerpt from each category post on different pages.
I am showing the post excerpt like so on each page and adding a read more link manually.
<?php query_posts('cat=4&showposts=1'); ?>
<?php while (have_posts()) : the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php the_excerpt(); ?>
Read More
<?php endwhile; ?>
The following code will then end the excerpt after the first paragraph.
function post_single_paragrapgh($text, $raw_excerpt) {
if( ! $raw_excerpt ) {
$content = apply_filters( 'the_content', get_the_content() );
$text = substr( $content, 0, strpos( $content, '</p>' ) + 4 );
}
return $text;
}
add_filter( 'wp_trim_excerpt', 'post_single_paragrapgh', 10, 2 );
What I would like to do is tell it to cut off after a second paragraph or, in fact an image and a paragraph.
It will pull in an image if there is one at the top of post but then I also want a further paragraph after the image or just two paragraphs of text. Either or.
I used this site for reference but method C1 throws up errors.
https://www.bybe.net/wordpress-the_excerpt-show-first-paragraph/
Thanks in advance!
Are you open to a solution with regex? Look for <p>(Anything)</p> and merge the two first occurrence. The code is not tested but should work.
function post_single_paragrapgh($text, $raw_excerpt) {
if( ! $raw_excerpt ) {
$content = apply_filters( 'the_content', get_the_content() );
preg_match_all( '~<p>(.*?)</p>~', $content, $matches );
if( isset( $matches[0][0] ) && isset( $matches[0][1] ) ) {
$text = $matches[0][0] . $matches[0][1];
} else {
$text = $matches[0][0];
}
}
return $text;
}
I managed to figure out what I needed with this code, posted the question too hastily I guess.
function awesome_excerpt($awesomeness_excerpt) {
global $post;
$raw_excerpt = $awesomeness_excerpt;
if ( '' == $awesomeness_excerpt ) {
$awesomeness_excerpt = get_the_content('');
$awesomeness_excerpt = strip_shortcodes( $awesomeness_excerpt );
$awesomeness_excerpt = apply_filters('the_content', $awesomeness_excerpt);
$awesomeness_excerpt = "<p>$awesomeness_excerpt</p>";
$wanted_number_of_paragraph = 2;
$tmp = explode ('</p>', $awesomeness_excerpt);
for ($i = 0; $i < $wanted_number_of_paragraph; ++$i) {
if (isset($tmp[$i]) && $tmp[$i] != '') {
$tmp_to_add[$i] = $tmp[$i];
}
}
$awesomeness_excerpt .= $excerpt_end;
return $awesomeness_excerpt;
}
return apply_filters('awesome_excerpt', $awesomeness_excerpt, $raw_excerpt);
}
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'awesome_excerpt');
Just took out the end if at the bottom - must have been left there in error. Hopefully it can help others.

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

Change Excerpt Length for Specific Page ID

I am working with a theme that uses the following function to set the post excerpt length on all pages:
//excerpt length
if ( !function_exists('custom_excerpt_length')):
function custom_excerpt_length( $length ) {
return 90;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
function new_excerpt_more( $more ) {
return '...';
}
add_filter('excerpt_more', 'new_excerpt_more');
endif;
I added new function:
add_filter( 'get_the_excerpt', 'so20668477_get_the_excerpt', 10, 1 );
function so20668477_get_the_excerpt( $excerpt )
{
if( is_page( 39370 ) )
$excerpt = apply_filters( 'the_content', get_the_content() );
return $excerpt;
}
But, it is not working, still showing post excerpt on page 39370. I am needing to display full length posts on Page ID = 39370 only. Not sure how to add the condition. Any help is much appreciated.
First you should be strongly encouraged to always use curly braces even in situations where they are technically optional.
Don't do:
if ( condition )
return false;
Instead do:
if ( condition ) {
return false;
}
About your question :
This function apply_filters( 'the_content', get_the_content() ); get the echo of the content not the content, so your condition is correct.
Try this code :
if( is_page( 39370 ) ){
$content = get_the_content();
$content = apply_filters('the_content', $content);
$excerpt = explode("[newpage]", $content);
}
return $excerpt;
Or there is another aproach :
if( is_page( 39370 ) ){
ob_start();
the_content();
$content = ob_get_clean();
}
return $excerpt;
I hope this solution will help you.

Categories