简体   繁体   中英

WordPress - Add Numeric Pagination.

I am using the Numeric Post Navigation code in my wordpress theme. I have set Blog page show at most as 3 for 24 posts. So that the page navigation displays 8 pages. eg: 1 2 3 ... 8 . But actually the page navigation shows me 1 2 3 ? 8 1 2 3 ? 8 . Now my question what is the error in the below code. Why its showing ? instead of dot. Please any one help me.

My code is,

function wpbeginner_numeric_posts_nav() {

    if( is_singular() )
        return;

    global $wp_query;

    /** Stop execution if there's only 1 page */
    if( $wp_query->max_num_pages <= 1 )
        return;

    $paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
    $max   = intval( $wp_query->max_num_pages );

    /** Add current page to the array */
    if ( $paged >= 1 )
        $links[] = $paged;

    /** Add the pages around the current page to the array */
    if ( $paged >= 3 ) {
        $links[] = $paged - 1;
        $links[] = $paged - 2;
    }

    if ( ( $paged + 2 ) <= $max ) {
        $links[] = $paged + 2;
        $links[] = $paged + 1;
    }

    echo '<div class="navigation"><ul>' . "\n";

    /** Previous Post Link */
    if ( get_previous_posts_link() )
        printf( '<li>%s</li>' . "\n", get_previous_posts_link() );

    /** Link to first page, plus ellipses if necessary */
    if ( ! in_array( 1, $links ) ) {
        $class = 1 == $paged ? ' class="active"' : '';

        printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );

        if ( ! in_array( 2, $links ) )
            echo '<li>…</li>';
    }

    /** Link to current page, plus 2 pages in either direction if necessary */
    sort( $links );
    foreach ( (array) $links as $link ) {
        $class = $paged == $link ? ' class="active"' : '';
        printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
    }

    /** Link to last page, plus ellipses if necessary */
    if ( ! in_array( $max, $links ) ) {
        if ( ! in_array( $max - 1, $links ) )
            echo '<li>…</li>' . "\n";

        $class = $paged == $max ? ' class="active"' : '';
        printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
    }

    /** Next Post Link */
    if ( get_next_posts_link() )
        printf( '<li>%s</li>' . "\n", get_next_posts_link() );

    echo '</ul></div>' . "\n";

}

Page Navigation in front end

<?php wpbeginner_numeric_posts_nav(); ?>

I think you might have some encoding problems, because I see that instead of using the 3 dots ... you are using the special character called "horizontal ellipsis" -> which combines 3 dots in a single character (try to select both dots provided and you will see the difference, the first example are 3 chars, and the second, 1 single char).

When the encoding is wrong and the browser cannot find a specif character in its characters table, it will just display it as a question mark by default.

There are several possible solutions:

Replace the character for simple dots in your code where this comment is:

/** Link to last page, plus ellipses if necessary */

Between the <li> 's


Try to set your page to UTF-8, putting this:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

In your <head> .


Use the html special char code to display that character instead of displaying it directly, which is called horizontal ellipsis and should be represented like this: &hellip;


You can use the first solution, or any/both of the last 2 solutions I provided to fix your problem

Note :

I just remembered that the encoding problem could come from php itself, so you could try adding this to your script just in case:

mb_internal_encoding("UTF-8"); Source : http://www.php.net/manual/en/function.mb-internal-encoding.php

The question mark may be a font-related issue. Some fonts, if they do not contain the written character on your page, display a question mark.

Make sure you typed the dot . in English keyboard layout in your source code. Even if the dot . looks the same across languages, for example French, if you typed it in French, if you use custom (web) font in your page - and this font does not contain the dot in French, you'll see a question mark.

put this code where you want to pagination

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

query_posts(array(
    'post_type'      => 'php', // You can add a custom post type if you like
    'paged'          => $paged,
    'posts_per_page' => 2
));

add the above code before starting post loop then start your loop

may it works for you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM