How to add custom CSS class name in PHP?

I am currently working on the code below and I’d like to add a custom class name. I’m not familiar with PHP.

How can I add a custom CSS class name?

<?php next_posts_link('Older Posts'); ?>
<?php previous_posts_link('Newer Posts'); ?>

Related posts

Leave a Reply

2 comments

  1. add_filter('next_posts_link_attributes', 'posts_link_attributes');
    add_filter('previous_posts_link_attributes', 'posts_link_attributes');
    
    function posts_link_attributes() {
        return 'class="styled-button"';
    }
    

    Add this in functions.php of your theme. It will add class “styled-button” to those links.
    If you would like to differ them, then use:

    add_filter('next_posts_link_attributes', 'next_posts_link_attributes');
    add_filter('previous_posts_link_attributes', 'prev_posts_link_attributes');
    
    function next_posts_link_attributes() {
        return 'class="next-styled-button"';
    }
    
    function prev_posts_link_attributes() {
        return 'class="prev-styled-button"';
    }
    
  2. I am not familiar with WordPress, but according to the Codex, there is no way to specify the class attributes in these function calls:

    What you can do is str_replace the html returned by these functions, e.g.

    echo str_replace('<a ', '<a class="" ', next_posts_link('Older Posts'));
    

    In case these functions already return a link with a class attribute, use a regular expression or DOM.