I recently bought a new Wordpress theme but all my custom excerpts are being cut-off based on character count rather than word count. I've found the excerpt.php file that the theme is using and I think the issue down comes down to the use of substr, but I can't figure out how to change it to fix it. I tried replacing it with wp_trim_words but then I just saw a number rather than text in my excerpts.
Would be grateful if someone can point me in the right direction!
The code:
$excerpt = get_the_excerpt();
if ( ! empty( $excerpt ) ) {
$excerpt_length = healthfirst_get_search_page_excerpt_length();
$new_excerpt = ( $excerpt_length > 0 ) ? substr( $excerpt, 0, intval( $excerpt_length ) ) : $excerpt;
?>
<p itemprop="description" class="qodef-e-excerpt">
<?php echo esc_html( strip_tags( strip_shortcodes( $new_excerpt ) ) ); ?>
</p>
<?php }
You can replace this line..
$new_excerpt = ( $excerpt_length > 0 ) ? substr( $excerpt, 0, intval( $excerpt_length ) ) : $excerpt;
.. with.
$new_excerpt = wp_trim_words( $excerpt, 10 );
You do not need the condition since you are trying to override the excerpt length. You can actually remove this line afterwards.
$excerpt_length = healthfirst_get_search_page_excerpt_length();
Unless it is still used somewhere else in that file or function.
Related
Im using the searchWP plugin in my wordpress site but the results of the keyword is showing html tags in the description. (see picture)
Is it possible to use a filter to remove those tags from showing up but still show the text without the tags?
The tags are inside the wordpress wysiwyg editor.
Update.
The search-form uses vue.
So if it is possible to trim the html tags from the results using vue i would prefer that.
I'm not tested try this.
function wpse159789_posts_search( $search, $query ) {
global $wpdb;
if ( ! preg_match( '/' . $wpdb->posts . '\.post_content LIKE \'%(.+)%\'/', $search, $matches, PREG_OFFSET_CAPTURE ) ) {
return $search;
}
$search_str = stripslashes( $matches[1][0] );
// Cater for closed angle pairs embedded in the search string.
for ( $i = 0, $len = mb_strlen( $search_str ); $i < $len; $i++ ) {
$q_searches[] = '(<[^>]*>)?' . preg_quote( mb_substr( $search_str, $i, 1 ) );
}
$q_search = implode( '', $q_searches );
$regexs[] = '^[^<]*' . $q_search; // Before any angle bracket.
$regexs[] = '(<[^>]*>)[^<]*' . $q_search; // After any closed angle bracket pair.
array_unshift( $regexs, implode( ' OR ', array_fill( 0, count( $regexs ), $wpdb->posts . '.post_content RLIKE %s' ) ) );
$search_replace = call_user_func_array( array( $wpdb, 'prepare' ), $regexs );
$search = substr( $search, 0, $matches[0][1] ) . $search_replace . substr( $search, $matches[0][1] + strlen( $matches[0][0] ) );
return $search;
}
I use Business Directory plugin for directory website. I want to restrict title to 100 characters or fewer.
I tried in CSS to restrict, but this doesn't work:
case 'title':
$value = sprintf( '<a href="%s" target="%s" >%s</a>',
get_permalink( $post_id ),
wpbdp_get_option( 'listing-link-in-new-tab' ) ? '_blank' : '_self',
get_the_title( $post_id ) );
break;
Use substr()
substr('get_the_title( $post_id )', 0, 100);
i just just have post and i try to use the excerpt. The problem is i dont want to have a lread more link. I just want too show the paragraph that have been excerpt. How to do that sir.
Use get_the_excerpt() to print an excerpt by specifying a maximium number of characters.
// Add below code in your functions.php
function the_excerpt_max_charlength($charlength) {
$excerpt = get_the_excerpt();
$charlength++;
if ( mb_strlen( $excerpt ) > $charlength ) {
$subex = mb_substr( $excerpt, 0, $charlength - 5 );
$exwords = explode( ' ', $subex );
$excut = - ( mb_strlen( $exwords[ count( $exwords ) - 1 ] ) );
if ( $excut < 0 ) {
echo mb_substr( $subex, 0, $excut );
} else {
echo $subex;
}
echo '[...]';
} else {
echo $excerpt;
}
}
// Pass character limit to the below function and display whenever you want.
the_excerpt_max_charlength(140);
I´m a bit out of my field here and I´m confused about this. Using wp_trim_field doesn't work for me except for regular strings.
This does not work, it returns the whole text:
<?php
$field = the_field('project_description');
$trimmedfield = wp_trim_words( $field, $num_words = 1, $more = '… ' );
echo '<p>' . $trimmedfield . '</p>';
?>
This however does work:
<?php
$field = 'this text does get trimmed';
$trimmedfield = wp_trim_words( $field, $num_words = 1, $more = '… ' );
echo '<p>' . $trimmedfield . '</p>';
?>
Echoing out the $field instead does echo out the text that I am trying to trim, but the trimming aint working. Any ideas as to why?
edit - I also tried this, same thing happens:
<?php
$length = 1;
$text = the_field('project_description');
$words = explode(' ', $text);
array_splice($words, $length);
$text = implode(' ', $words);
echo $text;
?>
use var_dump($field); wp_trim_words( $field,....) $field must be string type ...check if this is or not to test the datatype, if its not im sure you know what to do then.
Use typecast if its not.
You'll need to change the $field variable to this: $field = get_field('project_description');
the_field(); outputs the content, while get_field(); retrieves it. In order to pass it through a function, you'll need to retrieve it.
ACF documentation page that answers this question: https://www.advancedcustomfields.com/resources/displaying-custom-field-values-in-your-theme/
How can I get iso code language in WordPress?
This function :
get_bloginfo('language');
return me the languge like this : en-EN
I create a function like this :
<?php
function pr_language() {
$lang = get_bloginfo('language');
$pos = stripos($lang, '-');
$lang = substr(get_bloginfo('language'),0,-($pos+1)); // retourne "f"
return $lang;
}
?>
Is it correct? I want to display en not en-EN
I hope I understood your question. It seems like all you want to display is en-US.
According to WordPress,
Usage:
<?php bloginfo( $show ); ?>
Parameters:
language
So,
EXACT CODE:
<?php bloginfo('language'); ?>
Will output:
en-US
To show only en, just display first two characters.
function show_short_language() {
<?php echo substr( get_bloginfo ( 'language' ), 0, 2 );?>
}
Just a note for anyone in the future:
<?php echo substr( get_bloginfo ( 'language' ), 0, 2 );?>
Produced:
en
and
<?php echo substr( bloginfo ( 'language' ), 0, 2 );?>
Produced:
en-US
There are language codes with 3 letters rather than 2, so you shouldn't use substr. I did:
$lang = explode('-', get_bloginfo('language'));
$lang = $lang[0];