admin管理员组

文章数量:1323323

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.

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.

Share Improve this question edited Mar 4, 2013 at 18:24 Marc Wiest asked Mar 4, 2013 at 5:50 Marc WiestMarc Wiest 3721 gold badge4 silver badges16 bronze badges 2
  • You can use comments_number function to acheve this. codex.wordpress/Function_Reference/comments_number – Vinod Dalvi Commented Mar 4, 2013 at 6:02
  • I'm trying to set the $args in the functions file, so that all comments_number() functions have the same value. – Marc Wiest Commented Mar 4, 2013 at 6:42
Add a comment  | 

3 Answers 3

Reset to default 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;
  }

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.

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;
}

本文标签: commentsHow to use the 39commentsnumber39 filter