Support

Account

Home Forums General Issues Shortening the output of a text field for display

Solved

Shortening the output of a text field for display

  • Hello,

    I have a text area field on a custom post type called ‘news.’ In my template file, I’m pulling the title, the date, and some other data from the post and displaying it. The problem comes when I try to shorten the content of the post to a more manageable size. I could force a restriction in ACF when they input the text, but I need it all for the full post. Here’s what I’ve got right now:

    <?php                         
                            $args = array(
                                'posts_per_page' => 1,
                                'post_type' => 'news'
                            );                   
                            // get results
                            $the_query = new WP_Query( $args );
                            // The Loop
                            ?>
                <div>
                  <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
                    <?php $url = get_field('external_link');?>
                    <?php if( $url ): ?>
                  <h4>
                  <a href="<?php echo $url ?>">              
                    <?php the_title(); ?>
                    </a>
                  </h4>
                  <?php else: ?>
                  <h4>
                    <?php the_title(); ?>
                  </h4>
                  <?php endif; ?>
                  <p style="font-size:14;font-weight:300;">
                    <?php
    			  $date = DateTime::createFromFormat('Ymd', get_field('date'));
    				echo $date->format('m-d-Y');			  
    			  ?>,
                    <?php the_field('pub_name'); ?>
                  </p><small>            
                  <?php
    							$descr = the_field('description');
    							$descr = strip_tags($descr);
    							echo substr($descr, 0, 135); // pull only x amount of characters from the content
    							?>… </small>                          
                  <?php endwhile; ?>
                </div>
                <?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>

    The title is working, the date is working, and the pub_name is working. If I pull the ‘description’ field (starting with the $descr = the_field(‘description’) line), I cannot shorten it at all. The code above just outputs the entire thing, not the first 135 characters as I specified.

    The only thing I could think of is that if I echo the data type of the $descr variable, it comes out as ‘NULL’. I converted it to a string as a test, and that didn’t do any good. I’ve been wrestling with this for a couple of hours now and for the life of me, I can’t get the description to come out shorter than the full length. I searched the forums and found someone else using substr() and saying that it worked… I’m out of ideas.

    Thank you in advance

  • I kind of solved it:

    <small>            
                  <?php
    							$key_1_value = get_post_meta( get_the_ID(), 'description', true );
    							$key_1_value = strip_tags($key_1_value);
    							echo substr($key_1_value, 0, 130); // pull only x amount of characters from the content
    							?>… </small>

    If I use “get_post_meta”, it works just fine. The issue seems to be with using the_field() for some reason.

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Shortening the output of a text field for display’ is closed to new replies.