使当前/活动页码成为链接(WP_LINK_PAGES)

时间:2012-01-20 作者:AndrettiMilas

对于默认的wp\\U link\\U页面,当前/活动页码不是链接。我想找到一个代码/黑客,将当前/活动页面转换为指向页面顶部的链接。

我已经在为wp\\U link\\u页面使用自定义代码,可以在下面找到:

<?php
wp_link_pages(array(
    \'before\' => \'<p class="pagelinks">\' . __(\'\'),
    \'after\' => \'</p>\',
    \'next_or_number\' => \'next_and_number\', # activate parameter overloading
    \'nextpagelink\' => __(\'Next\'),
    \'previouspagelink\' => __(\'Previous\'),
    \'pagelink\' => \'%\',
    \'echo\' => 1 )
);
在我的功能中:

// Custom Next/Previous Page
add_filter(\'wp_link_pages_args\', \'wp_link_pages_args_prevnext_add\');
/**
 * Add prev and next links to a numbered link list
 */
function wp_link_pages_args_prevnext_add($args)
{
    global $page, $numpages, $more, $pagenow;

    if (!$args[\'next_or_number\'] == \'next_and_number\')
        return $args; # exit early

    $args[\'next_or_number\'] = \'number\'; # keep numbering for the main part
    if (!$more)
        return $args; # exit early

    if($page-1) # there is a previous page
        $args[\'before\'] .= _wp_link_page($page-1)
            . $args[\'link_before\']. $args[\'previouspagelink\'] . $args[\'link_after\'] . \'</a>\'
        ;

    if ($page<$numpages) # there is a next page
        $args[\'after\'] = _wp_link_page($page+1)
            . $args[\'link_before\'] . $args[\'nextpagelink\'] . $args[\'link_after\'] . \'</a>\'
            . $args[\'after\']
        ;

    return $args;
}
    ?>

2 个回复
最合适的回答,由SO网友:Joshua Abenazer 整理而成

看起来没有合适的挂钩可以根据您的需求修改输出。但您可以编写一个类似于wp_link_pages() 把这称为你的主题。下面的函数可以完成您的任务。

function custom_link_pages($args = \'\') {
        $defaults = array(
                \'before\' => \'<p>\' . __(\'Pages:\'), \'after\' => \'</p>\',
                \'link_before\' => \'\', \'link_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);
                                } elseif ( $i == $page ) {
                                    $output .= \'<a href="#">\';
                                }
                                $output .= $link_before . $j . $link_after;
                                if ( ($i != $page) || ( $i == $page ) || ((!$more) && ($page==1)) )
                                        $output .= \'</a>\';
                        }
                        $output .= $after;
                } else {
                        if ( $more ) {
                                $output .= $before;
                                $i = $page - 1;
                                if ( $i && $more ) {
                                        $output .= _wp_link_page($i);
                                        $output .= $link_before. $previouspagelink . $link_after . \'</a>\';
                                }
                                $i = $page + 1;
                                if ( $i <= $numpages && $more ) {
                                        $output .= _wp_link_page($i);
                                        $output .= $link_before. $nextpagelink . $link_after . \'</a>\';
                                }
                                $output .= $after;
                        }
                }
        }

        if ( $echo )
                echo $output;

        return $output;
}
此函数的使用方法与wp_link_pages().

EDIT

要同时包含页码和下一个/上一个链接,甚至应该将此过滤器添加到函数中。php。

// Custom Next/Previous Page
add_filter(\'wp_link_pages_args\', \'wp_link_pages_args_prevnext_add\');
/**
 * Add prev and next links to a numbered link list
 */
function wp_link_pages_args_prevnext_add($args)
{
    global $page, $numpages, $more, $pagenow;

    if (!$args[\'next_or_number\'] == \'next_and_number\')
        return $args; # exit early

    $args[\'next_or_number\'] = \'number\'; # keep numbering for the main part
    if (!$more)
        return $args; # exit early

    if($page-1) # there is a previous page
        $args[\'before\'] .= _wp_link_page($page-1)
        . $args[\'link_before\']. $args[\'previouspagelink\'] . $args[\'link_after\'] . \'</a>\'
    ;

    if ($page<$numpages) # there is a next page
        $args[\'after\'] = _wp_link_page($page+1)
        . $args[\'link_before\'] . $args[\'nextpagelink\'] . $args[\'link_after\'] . \'</a>\'
        . $args[\'after\']
    ;

    return $args;
}
要调用分页,可以将以下内容放入模板或循环中。

custom_link_pages(array(
    \'before\' => \'<p class="pagelinks">\' . __(\'\'),
    \'after\' => \'</p>\',
    \'next_or_number\' => \'next_and_number\', # activate parameter overloading
    \'nextpagelink\' => __(\'Next\'),
    \'previouspagelink\' => __(\'Previous\'),
    \'pagelink\' => \'%\',
    \'echo\' => 1 )
);

SO网友:Seb

我现在使用的一种简单方法是使用“link\\u before”和“link\\u after”作为wp\\u link\\u页面参数的一部分。然后,可以将每个数字(包括活动数字)包装在标记中,然后适当设置样式。

结束