My first question here:
I am using this code to show excerpts on my Wordpress. It allows me to show tags within the excerpt and it works OK, but the problem is when a have a word that is linked, after it I always get one space before the comma.
Example:
Google, Yahoo
Output:
Google , Yahoo
The space after the comma is on purpose and it should be there. The one before is one too much.
Any tips on how to fix this?
Code that I use in order to define excerpt:
function new_wp_trim_excerpt($text) {
$raw_excerpt = $text;
if ( '' == $text ) {
$text = get_the_content('');
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$text = strip_tags($text, '<a>');
$excerpt_length = apply_filters('excerpt_length', 70);
$words = preg_split('/(<a.*?a>)|\n|\r|\t|\s/', $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE );
if ( count($words) > $excerpt_length ) {
array_pop($words);
$text = implode(' ', $words);
$text = $text . $excerpt_more;
} else {
$text = implode(' ', $words);
}
}
return apply_filters('new_wp_trim_excerpt', $text, $raw_excerpt);
}
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'new_wp_trim_excerpt');
It appears to be an issue with your regex, if you add a comma after the <a.*?a> does that help? Here's the regex line with the comma included:
$words = preg_split('/(<a.*?a>,)|\n|\r|\t|\s/', $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE );
Related
Hi Im getting Fatal error: Uncaught TypeError: Unsupported operand types: WP_Post + int . I really dont know why this is happening. Im using the below filter to display link and html elements i want on excerpt. also to limit the amount of word displayed on the excerpt. However, when used $number parameter i get the error even though the value is interger .
$words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
this is the code that has the error.
so i put the the amount of word i want to display thorugh the apply filter below.
$topcontent = apply_filters( 'get_the_excerpt', '', 20 );
function custom_wp_trim_excerpt($text, $number) {
$raw_excerpt = $text;
// $countnumber = (int)($number);
// echo $countnumber;
if ( '' == $text ) {
//Retrieve the post content.
$text = get_the_content('');
//Delete all shortcode tags from the content.
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$allowed_tags = '<p>,<br>,<br/>,<br />,<a>,<em>,<strong>,<img>'; /*** MODIFY THIS. Add the allowed HTML tags separated by a comma.***/
$text = strip_tags($text, $allowed_tags);
$excerpt_word_count = $number;
/** MODIFY THIS. change the excerpt word count to any integer you like.***/
$excerpt_length = apply_filters('excerpt_length', $excerpt_word_count);
$excerpt_end = ' ' . '...' . '';
$excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end);
$words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
if ( count($words) > $excerpt_length && $words ) {
array_pop($words);
$text = implode(' ', $words);
$text = $text . $excerpt_more ;
} else {
$text = implode(' ', $words);
}
}
return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
}
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'custom_wp_trim_excerpt', 10, 2);
I want to know why im not able to use the value i get from the apply filter parameter and how can i solve it?
the line below refers to a hook not an integer value!
$excerpt_length = apply_filters('excerpt_length', $excerpt_word_count);
you may try:
//count the excerpt
$exlen = strlen( strip_tags( get_the_excerpt() ) );
$words = preg_split("/[\n\r\t ]+/", $text, $exlen + 1, PREG_SPLIT_NO_EMPTY);
I have created a test page in which users can access pdf and word documents.
site: http://recordandreturn2.insctest1.com/online-forms
The default search feature on the wordpress site does not bring up any of the items once you have typed them in. Example, Search "Affidavit of Heirship". I am using a plugin called easy table as the client will need to easily add or remove items moving forward. I have searched for wordpress search functionality enhancers, but nothing seems to work.
this example site has a search feature that brings up forms, this is what I want to achieve: http://www.judicialtitle.com/resources/forms
Here is a plugin capable of searching shortcodes:
https://wordpress.org/plugins/wp-ultimate-search/
Alternatively, you can add the following to functions.php to include shortcodes in your searches
<?php
//Replace wp_trim_excerpt with a commented out strip_shortcodes()
function improved_trim_excerpt($text) {
$raw_excerpt = $text;
if ( '' == $text ) {
$text = get_the_content('');
//$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$text = strip_tags($text);
$excerpt_length = apply_filters('excerpt_length', 55);
$excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
$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);
}
}
return apply_filters('improved_trim_excerpt', $text, $raw_excerpt);
}
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'improved_trim_excerpt');
//You might also need to add this in order to make sure the
//shortcodes are actually parsed and not just displayed
//$text = do_shortcode($text);
?>
This code is from http://3rdplanetwebsolutions.com/news/add-shortcode-content-to-wordpress-search-results/
I have a problem using the below:
<?php echo excerpt(15); ?>
Because! I have some pages (unfortunately) that have on-page styles and little text, not meeting the 15 characters, so what is occurring is the excerpt limit is not being met, so it displays 'a little text.. then <css> <styles><h1> <h2>, etc'.
How can I re-write as UP to 15 character limit, or MAX. So if it falls short that's fine. Or to exclude HTML / and CSS?
Excerpt function within functions.php
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;
}
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;
}
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 = 35;
$words = explode(' ', $text, $excerpt_length + 1);
if (count($words) > $excerpt_length) {
array_pop($words);
array_push($words, '...');
$text = implode(' ', $words);
}
}
return $text;
}
You have three options (possibly more, this isn't meant to be exhaustive)
Abandon the wordpress function. By using custom PHP passing the $post->post_content through to your own function you could use PHP's XML manipulation functions, specifically DOM manipulation, or a regular expression to remove certain tags.
Do a check on your post content before running excerpt. If you can check for the existence of a tag to start within the first 15 characters by checking for the first position of a "<" character using strpos, then you can use a normal if/else.
Using wordpresses functionality properly, you can change how the excerpt is dealt with through filters. You can remove the default functionality and replace it with your own. This link in particular is very useful for explaining how to customise your excerpt function in wordpress, specifically with regards to allowing tags or not within the excerpt.
I'm trying to figure out if it's possible to get an excerpt from each post, grabbing the first paragraph from each one. I'm currently using the ACF plugin and have custom post types and custom fields.
Here's my code:
function custom_field_excerpt() {
global $post;
$text = get_field('news');
if ( '' != $text ) {
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$excerpt_length = 20; // 20 words
$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
}
return apply_filters('the_excerpt', $text);
}
This works great, but it only trims the first 20 words (or however many words you specify), I'm trying to adjust this to pull in the first paragraph of each post instead of the first 20 words. Is this at all possible?
Because we know the content is bare (just text), you can simply explode the content via the \n character and then assume the first element in your new array is the first paragraph. You might be able to do this in a more efficient way but here is the function:
Your new function
function custom_field_excerpt() {
global $post;
$text = get_field('news');
if ( '' != $text ) {
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$text_paragraphs = explode("\n",$text);
$text = $text_paragraphs[0];
}
return $text;
}
This is the correct code, add it to your functions.php file
// Add custom excerpt length
function custom_excerpt($excerpt_length) {
$content = get_field('custom_field_name');
$text = strip_shortcodes( $content );
//$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$text = strip_tags($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);
}
echo $text;
}
Then use < ?php custom_excerpt('12'); ?> to specify the length in your template
(Remove the space in the php tag above)
And for anyone else who may find this - if you are using the_content and not ACF then just change
$content = get_field('blog_article_text');
TO
$content = get_the_content();
Try to replace $excerpt_length with strlen($text)
$text = wp_trim_words( $text, strlen($text), $excerpt_more );
I am new to php and am using the following code from http://aaronrussell.co.uk/legacy/improving-wordpress-the_excerpt/ to replace the the_excerpt() calls:
function improved_trim_excerpt($text) {
global $post;
if ( '' == $text ) {
$text = get_the_content('');
$text = apply_filters('the_content', $text);
$text = str_replace('\]\]\>', ']]>', $text);
$text = preg_replace('#<script[^>]*?>.*?</script>#si', '', $text);
$text = strip_tags($text, '<p>');
$excerpt_length = 80;
$words = explode(' ', $text, $excerpt_length + 1);
if (count($words)> $excerpt_length) {
array_pop($words);
array_push($words, '[...]');
$text = implode(' ', $words);
}
}
return $text;
}
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'improved_trim_excerpt');
I am using the Brandford Magazine 3.0 theme, and have two instances of the_excerpt() calls in my code.
Somehow the first call is getting replaced correctly and is displaying the text correctly as expected using improved_trim_excerpt, but the other call still uses the old the_excerpt() functionality.
What could I be missing here?
I don't see the_excerpt anywhere... but I'm guessing you're referring to the preg_replace? If so, try adding the g flag at the end of your regex. This turns on global mode, so the regex will match multiple times.