1

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.

0

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:

add_filter ('comments_number', 'wpse89257_comments_number', 10, 2);
function wpse89257_comments_number ($output, $number) {
  if ($number == 0) $output = '0 Comments';
  elseif ($number == 1) $output = '1 Comment';
  else $output = $number . ' Comments';
  return $output;
  }
0

If you put functions like get_permalink() or get_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 permalink

function my_personal_permalink( $anchor = '' ) {
  global $post;
  $return = get_permalink( $post->ID );
  $return .= $anchor;

  return $return;
}

This 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 with global $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.

  • The problem is not with the function, the function works. I'm looking for an example on how to use a filter with it. – Marc Wiest Mar 5 '13 at 0:08
  • You mean apply_filters() and add_filter()? – Ralf912 Mar 5 '13 at 8:50
0

From your comment on my first answer, I think you wish to apply filters.

function lpe_get_comments_number( $anchor='#comments' ) {
  $return = '<a class="comments-number-link" href="' . get_permalink() . $anchor . '">';

  $args = array(
    'zero' => 'Zero comments',
    'one' => 'One comment',
    'more' => '% Comments'
  );

  $filtered_args = apply_filters( 'choose_your_hook_name', $args );

  // be sure all keys in the filtered args are present and have a valid value
  // this is optional but usefull
  $args = wp_parse_args( $filtered_args, $args );

  $return .= get_comments_number( $args['zero'], $args['one'], $args['more'] ); // '0', '1', '%'

  $return .= '</a>';
  return $return;
}

add_filter( 'choose_your_hook_name', 'your_filter_callback', 1, 1 );

function your_filter_callback( $args ) {
  return array( 'zero' => 'Foo Comments', 'one' => 'Bar Comments' );
}

You can also pass additional argument to apply_filters() if you want to decide things depending on the passed arguments:

$zero = 'Zero Comments';

if ( $today == 'sunday' ) {
  $foo = 'Sunday, yeah!';
} else {
  $foo = '';
  $bar = 'No sunday';
}

$zero = apply_filters( 'choose_your_hook_name', $zero, $foo, $bar );

get_comments_number( $zero, $one, $more );

[more code]

add_filter( 'choose_your_hook_name', 'your_filter_callback', 1, 3 );

function your_filter_callback( $zero, $foo = '', $bar = '' ) {
  if ( ! empty( $foo ) )
    $zero = 'No comments on sunday!';
  elseif( ! empty( $bar ) )
    $zero = $bar . ', here are % comments';

  return $zero;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.