Limiting characters in $content | WordPress.org

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!

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer