WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using the Advanced Custom Field plugin - although I'm not positive that is relevant. Nonetheless, I can't get substr() to work at all using this method:

$givchars = 5; 
$postgiv = the_sub_field('get_involved_text'); 
$modgiv = substr($postgiv, 0, $givchars); 
echo ' ' .$modgiv. ' ';

However substr() does work when I do this:

$second_query = new WP_Query( $args );
if ( $second_query->have_posts() ):
   while( $second_query->have_posts() ) : $second_query->the_post();

   $titlechars = 5; // Character Limit
   $posttitle = get_the_title();
   $modtitle = substr($posttitle, 0, $titlechars);

   $attachment_id = get_field('image');
   $size = 'customfeatins'; // (thumbnail, medium, large, full or custom size)
   $image = wp_get_attachment_image_src( $attachment_id, $size ) . 'http://placehold.it/136x90';

    echo '<article>

        <img src="' . $image[0] . '" alt="' . get_the_title() .'" width="136" height="90" />
        <h3>' . $modtitle .'</h3>
        <p class="date">' . get_the_date() .'</p>



I'm just learning WordPress and my php skills are pretty limited, am I totally overlooking something?

share|improve this question
Ooops I pasted the wrong code there, that was something I found in another forum and it was all wrong... my bad! I have updated it. Sorry... – ByteMyPixel Dec 1 '12 at 0:13
What is 'it' in "it doesn't work"? substr? – s_ha_dum Dec 1 '12 at 1:46
yes, substr() - doesn't truncate the characters at all in the first example. substr() does truncate the characters in the second example. – ByteMyPixel Dec 1 '12 at 16:49
up vote 0 down vote accepted

It looks to me like the_sub_field echoes content. It is never returned for substr to work on. You need get_sub_field.

$givchars = 5; 
$postgiv = get_sub_field('get_involved_text'); 
$modgiv = substr($postgiv, 0, $givchars); 
echo ' ' .$modgiv. ' ';
share|improve this answer
Ahhh yes, that was it! Thank you so much for your help with this, and for the links to the documentation on get_sub_field, I had completely missed that! – ByteMyPixel Dec 1 '12 at 19:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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