简体   繁体   English

将范围添加到wordpress分页

[英]Add a range to wordpress post pagination

I have some very long posts, some have more than 50 pages() and when using wp_link_pages it give me 50 internal post links, clearly this isn't usable. 我有一些很长的帖子,有些有超过50个pages(),使用wp_link_pages时,它给了我50个内部帖子链接,显然这是不可用的。

I need to add a range to the wp_link_pages function, because as it stands now we output 50 paginated links. 我需要在wp_link_pages函数中添加一个范围,因为从现在开始,我们输出了50个分页链接。

The output should look like this: ...45*6*78... 输出应如下所示:... 45 * 6 * 78 ...

I've got the logic for adding the range but now I need to figure out how to add it to the existing wp_link_pages function? 我有添加范围的逻辑,但是现在我需要弄清楚如何将其添加到现有的wp_link_pages函数中?

Here is the example function: 这是示例函数:

function limitPagination(){
    $displayPages = 5;
    $firstPage = 1;
    $lastPage = $numpages > 5;

    while($numpages > $displayPages){
        if(($pageNow > $firstPage +2) && ($pageNow < $lastPage - 2)){
            echo (($pageNow - 2).($pageNow - 1).$pageNow.($pageNow + 1).($pageNow + 2));
        }
    }
}

This is the function I need to add my example function to: 这是我需要将示例函数添加到的函数:

// Add prev and next links to a numbered link list

function custom_wp_link_pages( $args = '' ) {
    $defaults = array(
        'before' => '<p id="post-pagination">' . __( 'Pages:' ), 
        'after' => '</p>',
        'text_before' => '',
        'text_after' => '',
        'next_or_number' => 'number', 
        'nextpagelink' => __( 'Next page' ),
        'previouspagelink' => __( 'Previous page' ),
        'pagelink' => '%',
        'echo' => 1
       );

    $r = wp_parse_args( $args, $defaults );
    $r = apply_filters( 'wp_link_pages_args', $r );
    extract( $r, EXTR_SKIP );

    global $page, $numpages, $multipage, $more, $pagenow;

    $output = '';
    if ( $multipage ) {
        if ( 'number' == $next_or_number ) {
            $output .= $before;
            for ( $i = 1; $i < ( $numpages + 1 ); $i = $i + 1 ) {
                $j = str_replace( '%', $i, $pagelink );
                $output .= ' ';
                if ( $i != $page || ( ( ! $more ) && ( $page == 1 ) ) )
                    $output .= _wp_link_page( $i );
                else
                    $output .= '<span class="current-post-page">';

                $output .= $text_before . $j . $text_after;
                if ( $i != $page || ( ( ! $more ) && ( $page == 1 ) ) )
                    $output .= '</a>';
                else
                    $output .= '</span>';
                }
        $output .= $after;
            } else {
                if ( $more ) {
                    $output .= $before;
                    $i = $page - 1;
                    if ( $i && $more ) {
                        $output .= _wp_link_page( $i );
                        $output .= $text_before . $previouspagelink . $text_after . '</a>';
                        }
                        $i = $page + 1;
                        if ( $i <= $numpages && $more ) {
                           $output .= _wp_link_page( $i );
                           $output .= $text_before . $nextpagelink . $text_after . '</a>';
                        }
                        $output .= $after;
                    }
                }
            }
        if ( $echo )
        echo $output;
        return $output;
}

The hook in your target function is this: 目标函数中的钩子是这样的:

$r = apply_filters( 'wp_link_pages_args', $r );

That means to run your limitPagination function you hook into wp_link_pages_args . 这意味着要运行您的limitPagination函数,您需要挂接到wp_link_pages_args It also means that your limitPagination function must accept $r as an argument and return a modified $r rather than echoing the output. 这也意味着您的limitPagination函数必须接受$r作为参数并返回修改后的$r而不是回显输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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