I’m not sure how to use this filter. Could someone show me an example? I’m trying to change ('0', '1', '%')
to ('0 Comments', '1 Comment', '% Comments')
.
I’m using this function to get the comments number.
function lpe_get_comments_number( $anchor='#comments' ) {
$return = '<a class="comments-number-link" href="' . get_permalink() . $anchor . '">';
$return .= get_comments_number(); // '0', '1', '%'
$return .= '</a>';
return $return;
}
I know I could just set the arguments here and call it a day, but I was hoping to store my custom functions away in a different file and handle their settings from the themes functions.php.
The filter you mention is used by the end of
get_comments_number_text
. As you can see from the source the number of comments is passed through the filter, allowing you to completely change the default text. Like this:If you put functions like
get_permalink()
orget_comments_number()
in a function, then you have to make the$comments
and$post
vars accessible. Read more about the scope in PHP.Normally you will do it with a
global
keyword. An easy example: retriving the permalinkThis is a very simple example.
get_permalink()
need some information from which post it should generate the permalink. So we have to make the ‘post’ accessible inside the function. This will be done withglobal $post;
, it will make the actual post data accessible.get_permalink()
need the post ID, we pass it with$post->ID
If you use template functions inside a function, then you have to explore which data the template functions need (post, comment, database connection (
$wpdb
), etc.) and pass them as argument.From your comment on my first answer, I think you wish to apply filters.
You can also pass additional argument to
apply_filters()
if you want to decide things depending on the passed arguments:[more code]