Wordpress Themes - WP Forum at BFA

Wordpress Themes - WP Forum at BFA (http://forum.bytesforall.com/index.php)
-   Excerpts, Read more, Pagination (http://forum.bytesforall.com/forumdisplay.php?f=20)
-   -   Manual excerpt still cutting last word (http://forum.bytesforall.com/showthread.php?t=5330)

buzz628 Jan 15, 2010 04:59 PM

Manual excerpt still cutting last word
 
I read through the posts that discussed this issue and the solution was to use the "[BUGFIX 3.4.1/3.4.2/3.4.4] Getting Configure Excerpts to work in all cases."

Since I have the newest version of Atahualpa (3.4.5.1) installed that fix has already been applied. Yet, I still have the problem where the last word in my manual excerpt does not show up for each post.

I can add a random word to the end and it will then add that last word I want but I was hoping there was a real fix somewhere.

I also have Wordpress 2.9.1 installed

chiara7 Jan 16, 2010 04:15 PM

I have the same problem! (With atahualpa 3.4.5.1 or 3.4.4 and WordPress 2.9.1). If I turn to Atahualpa 3.4.1, the manual excerpt is shown perfectly with all words.

IanS Jan 17, 2010 12:10 PM

I was having the same issue, and I found a slightly simpler workaround than leaving an extra word.

Just leave a space after the last word. That worked for me.

blaise Feb 6, 2010 05:39 PM

In fact, this problem occurs because of some inconsistent logic in the same function that's mentioned in "[BUGFIX 3.4.1/3.4.2/3.4.4] Getting Configure Excerpts to work in all cases."

When faking an excerpt from content, the function bfa_wp_trim_excerpt cleverly counts the words in the article and, if the wordcount is over the limit, removes the excess words and adds the "read more" link.

This almost works, almost by accident, when using real excerpts, because the limit looks like zero, so the "read more" link always gets added, which is what we want. Unfortunately, since the function "thinks" the excerpt has excess text, it also removes the last word, unless the article ends in a space.

A quick fix is to make the conditions explicit. In functions.php in the Atahualpa theme directory, replace
Code:

function bfa_wp_trim_excerpt($text) { // Fakes an excerpt if needed
       
  global $bfa_ata;

  if ( '' == $text ) {
    $text = get_the_content('');
    $text = apply_filters('the_content', $text);
    $text = str_replace(']]>', ']]>', $text);
    $text = strip_tags($text, $bfa_ata['dont_strip_excerpts']);
    $excerpt_length = $bfa_ata['excerpt_length'];
    $words = explode(' ', $text, $excerpt_length + 1);
  } else {
    $words = explode(' ', $text);
  }

  if (count($words) > $excerpt_length) {       
    array_pop($words);       
    $custom_read_more = str_replace('%permalink%', get_permalink(), $bfa_ata['custom_read_more']);
    $custom_read_more = str_replace('%title%', the_title('','',FALSE), $custom_read_more);
    array_push($words, $custom_read_more);
    $text = implode(' ', $words);
  }

    return $text;
}

by
Code:

function bfa_wp_trim_excerpt($text) { // Fakes an excerpt if needed
       
  global $bfa_ata;

  if ( '' == $text ) {
    $text = get_the_content('');
    $text = apply_filters('the_content', $text);
    $text = str_replace(']]>', ']]>', $text);
    $text = strip_tags($text, $bfa_ata['dont_strip_excerpts']);
    $excerpt_length = $bfa_ata['excerpt_length'];
    $words = explode(' ', $text, $excerpt_length + 1);
    $faked_excerpt = true; // Drop excess words in a faked excerpt (see below)
  } else {
    $words = explode(' ', $text);
    $excerpt_length = 0; // Force the readmore link when using a real excerpt
    $faked_excerpt = false; // Do NOT drop the last word of a real excerpt (see below)
  }

  if (count($words) > $excerpt_length) {       
    if($faked_excerpt) array_pop($words); // Drop excess words ONLY IF EXCERPT IS FAKED
    $custom_read_more = str_replace('%permalink%', get_permalink(), $bfa_ata['custom_read_more']);
    $custom_read_more = str_replace('%title%', the_title('','',FALSE), $custom_read_more);
    array_push($words, $custom_read_more);
    $text = implode(' ', $words);
  }

  return $text;
}

All the added/changed lines have comments. If you don't understand the code, look at the documentation of the limit option for the php function explode.

I didn't want to tweak the code too much... However, it would be cleaner to separate the two cases. Pseudo code:
Code:

If there IS a real excerpt (
  Set the "show read more" flag
)
If there is NO real excerpt (
  If there are more than max-length words (
    Remove the excess words
    Set the "show read more" flag
  )
  If less than max-length words (
    Do NOT set the "show read more" flag
  )
)
 If the "show read more" flag is set (
  Generate and add the "read more" text
)

This approach would also save a -- probably insignificant -- amount of execution time when using real excerpts and when the article is shorter than the limit.

Hope this helps.

chiara7 Feb 7, 2010 01:39 PM

Wow! Thank you! It works.

juggledad Feb 8, 2010 07:35 AM

Blaise,

Good find! !!

When I was coding the original fix, I must have missed the manual excerpts, there are so many cases that make this a mess. I'll see if I can get Flynn to get this into the next release.


All times are GMT -6. The time now is 11:22 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.