Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

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
add comment

1 Answer 1

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
add comment

Your Answer

 
discard

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.