• Resolved thetoolman123

    (@thetoolman123)


    Hi there,

    I have the following code which displays some content from a directory listing in WP. I would like to limit the number of characters that are displayed.

    <?php
    
    if ( ! defined('ABSPATH') ) {
    	exit;
    }
    
    if ( empty( $options['info_fields'] ) ) {
    	return;
    } ?>
    
    <!-- <ul class="lf-contact">-->
    	<?php foreach ( (array) $options['info_fields'] as $info ) {
            $string = $info['label'];
            $attributes = [];
    
            if ( $is_caching ) {
                list( $string, $attributes, $cls ) = \MyListing\prepare_string_for_cache( $string, $listing );
            }
    
            $content = do_shortcode( $listing->compile_string( $string ) );
            if ( ! empty( $content ) ) { ?>
                <!--<li <?php echo join( ' ', $attributes ) ?>>-->
                	<?php if ( ! empty( $info['icon'] ) ): ?>
                    	<i class="<?php echo esc_attr( $info['icon'] ) ?> sm-icon"></i>
                	<?php endif ?>
                    <p><?php echo $content ?></p>
    <!--            </li>-->
            <?php }
    	} ?>
    <!--</ul>-->
    

    I have tried the following, but is then inserting the same content and from an entirely different post.

    // Get the post content
    $content = get_the_content();
    
    // Define the maximum number of characters to display
    $limit = 150;
    
    // Trim the content to the desired character limit
    $trimmed_content = wp_trim_words( $content, $limit, '...' );
    
    // Output the trimmed content
    echo $trimmed_content;
        

    Any ideas how I can limit the characters on the above original code?

    Many thanks!

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Limiting characters in $content’ is closed to new replies.