Php string not working in Wordpress Functions.php (trying to fetch 1st category for each blog that post appears in the sidebar)

I am trying to fetch 1st category for each blog post that appears in the sidebar of any blogpost landing page.

I tried multiple options to add the 2nd block code to the 1st block at the line .wp_get_post_categories ($post_id). but I could not find any solution.

Please help me with that and if you need closer to the code just let me know.

1st Block

$related_blog .=div class='related-blog'
                            div class='row'
                                a href=.get_the_permalink().
                            
                                    div class='col-lg-4 col-md-4 col-xs-4 blog-image'
                                        img src=.$image_related[0].
                                    /div

                                    div class='col-lg-8 col-md-8 col-xs-8 blog-text'
                                        strongp class='related-blog-title'.get_the_title()./p/strong
                                /a
                                span class='related-blog-sidebar-content '.substr(get_the_content(),0,250)..../span
                                
                                p class='related-blog-sidebar-category'
                                .wp_get_post_categories ($post_id).                               
                                /p
                                
                                a class='related-blog-sidebar-link' href=.get_the_permalink().Read More
                                /a

                                    /div
                            /div
                        /div;

2nd Block

?php $category = get_the_category(); ?
a  href=?php echo get_category_link($category[0]-cat_ID); ? 
?php echo $category[0]-cat_name; ? /a

Topic function-exists link-category functions categories Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.