Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Title is little bit :S sorry :) For example,we have paginate links function that output html something like this

<span class='page-numbers current'>some number</span>

How can I filter that,to remove that span,class,to apply my own html tags,styles,tnx in advance.I just want to apply my own styles and html tags,is this possible?

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

Use a custom pagination function:

my source:

function fb_paging_bar( $args = array() ) {

    $defaults = array(
        'range'           => 4,
        'custom_query'    => FALSE,
        'previous_string' => __( '&laquo;&laquo;&laquo;', FB_GREYFOTO_TEXTDOMAIN ),
        'next_string'     => __( '&raquo;&raquo;&raquo;', FB_GREYFOTO_TEXTDOMAIN ),
        'view_fp'         => TRUE,
        'view_lp'         => TRUE,
        'before_output'   => '<div class="postlink">',
        'after_output'    => '</div>'
    );

    $args = wp_parse_args( 
        $args, 
        apply_filters( 'fb_paging_bar_defaults', $defaults )
    );

    $args['range'] = (int) $args['range'] - 1;
    if ( !$args['custom_query'] )
        $args['custom_query'] = @$GLOBALS['wp_query'];
    $count = (int) $args['custom_query']->max_num_pages;
    $page  = intval( get_query_var( 'paged' ) );
    $ceil  = ceil( $args['range'] / 2 );

    if ( $count <= 1 )
        return FALSE;

    if ( !$page )
        $page = 1;

    if ( $count > $args['range'] ) {
        if ( $page <= $args['range'] ) {
            $min = 1;
            $max = $args['range'] + 1;
        } elseif ( $page >= ($count - $ceil) ) {
            $min = $count - $args['range'];
            $max = $count;
        } elseif ( $page >= $args['range'] && $page < ($count - $ceil) ) {
            $min = $page - $ceil;
            $max = $page + $ceil;
        }
    } else {
        $min = 1;
        $max = $count;
    }

    $echo = '';
    $previous = intval($page) - 1;
    $previous = esc_attr( get_pagenum_link($previous) );
    if ( $previous && (1 != $page) )
        $echo .= '<a href="' . $previous . '" title="' . __( 'previous', FB_GREYFOTO_TEXTDOMAIN) . '">' . $args['previous_string'] . '</a>';
    $firstpage = esc_attr( get_pagenum_link(1) );

    if ( $args['view_fp'] && $firstpage && (1 != $page) )
        $echo .= '<a href="' . $firstpage . '">' . __( 'First', FB_GREYFOTO_TEXTDOMAIN ) . '</a>';

    if ( !empty($min) && !empty($max) ) {
        for( $i = $min; $i <= $max; $i++ ) {
            if ($page == $i) {
                $echo .= '<span class="active">' . str_pad( (int)$i, 2, '0', STR_PAD_LEFT ) . '</span>';
            } else {
                $echo .= sprintf( '<a href="%s">%002d</a>', esc_attr( get_pagenum_link($i) ), $i );
            }
        }
    }

    if ($args['view_lp']) {
        $lastpage = esc_attr( get_pagenum_link($count) );
        if ( $lastpage && ($count != $page) ) {
            $count = str_pad( (int)$count, 2, '0', STR_PAD_LEFT );
            $echo .= '<a href="' . $lastpage . '">' . __( 'Last', FB_GREYFOTO_TEXTDOMAIN ) . '(' . $count . ')' . '</a>';
        }
    }

    $next = intval($page) + 1;
    $next = esc_attr( get_pagenum_link($next) );
    if ($next && ($count != $page) )
        $echo .= '<a href="' . $next . '" title="' . __( 'next', FB_GREYFOTO_TEXTDOMAIN) . '">' . $args['next_string'] . '</a>';

    if ( isset($echo) )
        echo $args['before_output'] . $echo . $args['after_output'];
}

also you can hook inside the function for custom format and variables.

also an example:

function change_fb_paging_bar_defaults($args) {

    $args['previous_string'] = ''; // empty string
    $args['next_string'] = '';

    return $args;
}
add_filter('fb_paging_bar_defaults', 'change_fb_paging_bar_defaults');

in your template sonly use the function:

fb_paging_bar();
share|improve this answer
    
Tnx,this looks great. –  user1147 Dec 9 '10 at 12:20
add comment

I think the best way to do it would be to copy that function (wp-includes/general-template.php, line 1943) and create your own with the markup you want.

share|improve this answer
    
I did that,but I'm not happy with that :( maybe some regex can fix issue? paginate links is just example function,there are many,many more function that we sometimes need to change default markup.Tnx for response –  user1147 Dec 6 '10 at 7:56
1  
You can use the argument 'type' => 'array' to return the pagination links as an array and use a replace function on each element to change the markup. But I would not recommend that for this function because I think it is too much processing. If you have other functions, you should deal with them on a case by case basis and use str_replace, or preg_replace, or copy the function, or use a filter included in the function. You should find the best solution performance-wise for each function, not a one-size fit-all solution. And I think a copy is the best for this, not a regex or other replace. –  sorich87 Dec 6 '10 at 8:09
    
small hint: WPEngineer.com publish in the next view days an custom pagination function to use in theme –  bueltge Dec 6 '10 at 22:20
add comment

There are several possible ways to customize markup, it depends on specific function:

  • lookup function on deeper level that is used to retrieve data, use it with own markup;
  • check if output is passed through filter, use filter to make replacements;
  • markup is generated with walker class, create own walker and pass it to function.

There are probably more ways, but I think these are common.

Overall I think WP markup is reasonably usable and semantic. Most cases I had too mess with it are JS stuff requiring very specific markup.

Did you consider it may be more consistent and viable to make CSS fit generic markup?

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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