0

I'm trying to do is the following. If the excerpt length is shorter than 30 charakters, than do..

<?php if ( get_the_excerpt() <= 30 AND has_post_thumbnail() ) : ?>
   <div></div>
<?php else : ?>
   <div></div>
<?php endif; ?>

But it seems like that's not the way to read out the length. I was playing around with some definitions that I found like:

<?php if ( the_excerpt() <= 30 AND has_post_thumbnail() ) : ?>
<?php if ( $count($the_excerpt) <= 30 AND has_post_thumbnail() ) : ?>
<?php if ( excerpt_length($count) <= 30 AND has_post_thumbnail() ) : ?>

..but I'm helpless. So..you kind guys. Any idea?

1

This is more PHP as you need to use str_word_count() to count the amount of words in the excerpt. Just note, to be safe, if you allow any tags in the excerpt, you would want to use strip_tags() to remove html tags to avoid incorrect word counts.

EXAMPLE:

echo str_word_count( strip_tags( get_the_excerpt() ) );

If you need to display wordcount on the front end, you can also wrap the above in number_format_i18n() to return the integer value according to locale set

echo number_format_i18n( str_word_count( strip_tags( get_the_excerpt() ) ) );
0

The answer is basic PHP:

$exc = get_the_excerpt();
// var_dump($exc);
$exc = strlen($exc);
// var_dump($exc);
if ( $exc <= 30 AND has_post_thumbnail() ) {

} else {

}

You need to count the string's characters before trying to compare the string to an integer. PHP's strlen() does that.

However, you also misunderstand a few things...

if ( the_excerpt() <= 30 AND has_post_thumbnail() ) :

the_excerpt() prints content. All that will do is echo the excerpt to the screen. There is no data "captured" to operate on.

if ( $count($the_excerpt) <= 30 AND has_post_thumbnail() )

The leading $ makes this a function. While you can create a variable function in PHP, I doubt you have. Without the $, count() will just return 1 with a string. It doesn't count the characters.

if ( excerpt_length($count) <= 30 AND has_post_thumbnail() )

excerpt_length doesn't work that way. In fact, it isn't even a function. It is a filter.

I'd suggest that if you are going to be hacking your site, get familiar with the PHP Docs and the Codex, and stop trying to guess at functions, infer from other languages, or just make things up. This may be helpful too.

  • Wow thank you very much for this explanation!! I know I have to learn more about all this. even more now that you mention the points, identifying a function, filter and so on. Never thought i I would be hacking this deep into the site :D Thanks for the links! This will be my evening read :) – ezkay Dec 2 '15 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.