Problem with PHP code in latest PHP version

I am helping out a friend on his website and there are problems upgrading to the latest version of PHP… I feel like the problem is in the code below… I can’t seem to work it out. Any suggestions? Thank you in advance!

 <?php
                $args = array(
                    'category_name' => news,
                );
                $newsQuery = new WP_Query($args);
                if ($newsQuery->have_posts()) : while ($newsQuery->have_posts()) : $newsQuery->the_post();
                ?>

                    <div class="col-lg-4 col-md-6 mb-3">
                        <div class="card">
                            <div class="card-img"
                                 style="background-image: url('<?php echo wp_get_attachment_url(get_post_thumbnail_id($post->ID)); ?>');"></div>
                            <div class="card-body">
                                <h5 class="card-title"><?php the_title() ?></h5>
                                <p class="card-text"><?php echo excerpt(10) ?></p>
                                <a href="<?php the_permalink() ?>" class="btn btn-primary">
                                    View more
                                </a>
                            </div>
                        </div>
                    </div>

                <?php endwhile; else : ?>

                    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>

                <?php endif; ?>

The potential issue might be with the function excerpt(10). WordPress does not have a native excerpt() function that takes an argument like that. If this is a custom function defined in the theme or a plugin, you would need to make sure that function is defined and working properly. If you want to use WordPress’s native excerpt functionality, you might replace <?php echo excerpt(10) ?> with <?php the_excerpt(); ?>.

So, the corrected code looks like this:

<?php
$args = array(
    'category_name' => 'news',
);
$newsQuery = new WP_Query($args);
if ($newsQuery->have_posts()) : while ($newsQuery->have_posts()) : $newsQuery->the_post();
?>

    <div class="col-lg-4 col-md-6 mb-3">
        <div class="card">
            <div class="card-img"
                 style="background-image: url('<?php echo wp_get_attachment_url(get_post_thumbnail_id($post->ID)); ?>');"></div>
            <div class="card-body">
                <h5 class="card-title"><?php the_title() ?></h5>
                <p class="card-text"><?php the_excerpt(); ?></p>
                <a href="<?php the_permalink() ?>" class="btn btn-primary">
                    View more
                </a>
            </div>
        </div>
    </div>

<?php endwhile; else : ?>

    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>

<?php endif; ?>

Sponsor our Newsletter | Privacy Policy | Terms of Service