0

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>

    </article>
    ';

   endwhile;
endif;

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

3
  • 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... Commented Dec 1, 2012 at 0:13
  • What is 'it' in "it doesn't work"? substr?
    – s_ha_dum
    Commented Dec 1, 2012 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. Commented Dec 1, 2012 at 16:49

1 Answer 1

0

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. ' ';
1
  • 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! Commented Dec 1, 2012 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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