wordpress Next/Previous post in same category not working

I use the following code to Displays Next/Previous link in same posts category and work correctly for me.

<?php


 get_template_part( 'content', get_post_format() );

 // Previous/next post navigation.
 previous_post_link('%link"https://wordpress.stackexchange.com/questions/344307/,"Before', true );
 next_post_link( '%link"https://wordpress.stackexchange.com/questions/344307/,"Next', true );

 ?>

And For CSS Styling the “%link” Element I use this code in my functions.php file.

  /////Next/Prev post style

  add_filter('next_post_link"https://wordpress.stackexchange.com/questions/344307/,"next_post_link_attributes');
  add_filter('previous_post_link"https://wordpress.stackexchange.com/questions/344307/,"previous_post_link_attributes');

   function next_post_link_attributes($output) {/// Styling next link

     $injection = 'class="ls-left w3-btn"';
     return str_replace('<a href="https://wordpress.stackexchange.com/questions/344307/,"<a '.$injection.' href=", $output);

    }

  function previous_post_link_attributes($output) {/// Styling previous link

   $injection = "class="ls-right w3-btn prev"';
   return str_replace('<a href="https://wordpress.stackexchange.com/questions/344307/,"<a '.$injection.' href=", $output);

   }

Top code: I use 2 different functions “previous_post_link_attributes” and “next_post_link_attributes” Because I want to add different CSS style for nex/pre links

If you want to add the same style to next/pre links you can use one method:

add_filter("next_post_link"https://wordpress.stackexchange.com/questions/344307/,"post_link_attributes');
add_filter('previous_post_link"https://wordpress.stackexchange.com/questions/344307/,"post_link_attributes');

function post_link_attributes($output) {
$injection = 'class="my-class"';
return str_replace('<a href="https://wordpress.stackexchange.com/questions/344307/,"<a '.$injection.' href=", $output);
}

For more about next / pre link –> wpcodex ,masternsblog ,wp-stack