1

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'); ?>
2
  • 4
    Sorry, but this question is impossible to answer without seeing the code for next_posts_link and previous_posts_link. Commented Jan 7, 2013 at 8:30
  • Do you use some kind of CMS? Commented Jan 7, 2013 at 8:34

2 Answers 2

3
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"';
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @Marcin for your help! It really solved my problem! :) Thank you very much also to those who tried to help :) Have a great day everyone!
1

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.

4 Comments

Thats not a elegant way :(. There are filters for that kind of things ;).
@MarcinBobowski Like I said, I am not familiar with WP so I suggested the naive approach. On a sidenote, I'd never use the words elegant and Wordpress in one sentence. There is no elegance in Wordpress.
Depends on what You like :). I love to code in Wordpress. Haven't found any CMS that have so great documentation and so big community support (but I might be wrong, and there might be better ones) with so meny possibilities to modify CMS without editing it's core files. If there are any other, that could fit also, please provide me a name.
@MarcinBobowski Wordpress is very hackable. That's part of it's success story. It's just that hackable != elegant. WP is infamous for it's messy codebase. That's all I'm saying.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.