设置帖子的导航链接(第一个、上一个、下一个、最后一个)

时间:2018-11-04 作者:Kohlo

我正在为我的网络喜剧/图形小说设置wordpress,但在导航方面遇到了一些小问题。以下是我想要的,一个非常基本的网络经济导航方案:

<<;首先|<;上一个|下一个|上一个>>

每个页面都会动态链接到各自的漫画页面(这是独立的帖子)。理想情况下,我不会在实际的最新页面上显示“Last”链接。

我一直在处理这些函数:

previous\\u post\\u link();next\\u post\\u link();

显示如下导航,例如:

«第2页第4页»

(当我在第3页时,“第2页”和“第4页”是帖子标题)

我想这并不理想,但有点效果。我相信有一种方法可以让这些链接说出我想要他们说什么。这是我真正遇到麻烦的“最后”部分。

如果可能的话,我宁愿不使用网络喜剧或喜剧画架插件,因为基本的Wordpress对于我想要的东西非常有用。

3 个回复
SO网友:sandrodz

gist 应该让你开始。

<?php
/*
Plugin Name:    Easy Pagination Deamon
Plugin URI:     http://wordpress.org/extend/plugins/stats/
Description:    Offers the deamon_pagination($range) template tag for a sematically correct pagination.
Author:         Franz Josef Kaiser
Author URI:     http://say-hello-code.com
Version:        0.1
License:        GPL v2 - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
Text Domain:    pagination_deamon_lang

Copyright 20010-2011 by Franz Josef Kaiser

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
*/

// ==========================================
   Styles see https://gist.github.com/818523
// ==========================================

// Secure: don\'t load this file directly
if( !class_exists(\'WP\') ) :
    header( \'Status: 403 Forbidden\' );
    header( \'HTTP/1.1 403 Forbidden\' );
    exit();
endif;

!defined(\'PAGE_LANG\') ? define( \'PAGE_LANG\', \'pagination_deamon_lang\' ) : wp_die(\'The constant PAGE_LANG is already defined.\');
!defined(\'PAGE_VERSION\') ? define( \'PAGE_VERSION\', 0.1 ) : wp_die(\'The constant PAGE_VERSION is already defined.\');
!defined(\'PAGE_PATH\') ? define( \'PAGE_PATH\', trailingslashit(WP_PLUGIN_URL.\'/\'.str_replace(basename( __FILE__),"",plugin_basename(__FILE__))) ) : wp_die(\'The constant PAGE_PATH is already defined.\');

    /**
     * Register styles
     */
    if ( !is_admin() ) {
        wp_register_style( \'pagination\', PAGE_PATH.\'pagination.css\', false, PAGE_VERSION, \'screen\' );
    }

    if ( !function_exists(\'oxo_pagination_styles\') ) :
    /**
     * Print styles
     */
    function oxo_pagination_styles() {
        if ( !is_admin() )
            wp_print_styles(\'pagination\');
    }
    endif;

    if ( !function_exists(\'oxo_pagination\') ) :
    /**
     * Wordpress pagination for archives/search/etc.
     * 
     * Semantically correct pagination inside an unordered list
     * 
     * Displays: First « 1 2 3 4 » Last
     * First/Last only appears if not on first/last page
     * Shows next/previous links «/»
     * Accepts a range attribute (default = 5) to adjust the number
     * of direct page links that link to the pages above/below the current one.
     * 
     * @param (int) $range
     */
    function oxo_pagination( $range = 5 ) {
        // $paged - number of the current page
        global $paged, $wp_query;
        // How much pages do we have?
        if ( !$max_page )
            $max_page = $wp_query->max_num_pages;
        // We need the pagination only if there is more than 1 page
        if ( $max_page > 1 )
            if ( !$paged ) $paged = 1;

        echo "\\n".\'<ul class="pagination">\'."\\n";
            // On the first page, don\'t put the First page link
            if ( $paged != 1 )
                echo \'<li class="page-num page-num-first"><a href=\'.get_pagenum_link(1).\'>\'.__(\'First\', PAGE_LANG).\' </a></li>\';

            // To the previous page
            echo \'<li class="page-num page-num-prev">\';
                previous_posts_link(\' &laquo; \'); // «
            echo \'</li>\';

            // We need the sliding effect only if there are more pages than is the sliding range
            if ( $max_page > $range ) :
                // When closer to the beginning
                if ( $paged < $range ) :
                    for ( $i = 1; $i <= ($range + 1); $i++ ) {
                        $class = $i == $paged ? \'current\' : \'\';
                        echo \'<li class="page-num"><a class="paged-num-link \'.$class.\'" href="\'.get_pagenum_link($i).\'"> \'.$i.\' </a></li>\';
                    }
                // When closer to the end
                elseif ( $paged >= ( $max_page - ceil($range/2)) ) :
                    for ( $i = $max_page - $range; $i <= $max_page; $i++ ){
                        $class = $i == $paged ? \'current\' : \'\';
                        echo \'<li class="page-num"><a class="paged-num-link \'.$class.\'" href="\'.get_pagenum_link($i).\'"> \'.$i.\' </a></li>\';
                    }
                endif;
            // Somewhere in the middle
            elseif ( $paged >= $range && $paged < ( $max_page - ceil($range/2)) ) :
                for ( $i = ($paged - ceil($range/2)); $i <= ($paged + ceil($range/2)); $i++ ) {
                        $class = $i == $paged ? \'current\' : \'\';
                    echo \'<li class="page-num"><a class="paged-num-link \'.$class.\'" href="\'.get_pagenum_link($i).\'"> \'.$i.\' </a></li>\';
                }
            // Less pages than the range, no sliding effect needed
            else :
                for ( $i = 1; $i <= $max_page; $i++ ) {
                    $class = $i == $paged ? \'current\' : \'\';
                    echo \'<li class="page-num"><a class="paged-num-link \'.$class.\'" href="\'.get_pagenum_link($i).\'"> \'.$i.\' </a></li>\';
                }
            endif;

            // Next page
            echo \'<li class="page-num page-num-next">\';
                next_posts_link(\' &raquo; \'); // »
            echo \'</li>\';

            // On the last page, don\'t put the Last page link
            if ( $paged != $max_page )
                echo \'<li class="page-num page-num-last"><a href=\'.get_pagenum_link($max_page).\'> \'.__(\'Last\', PAGE_LANG).\'</a></li>\';

        echo "\\n".\'</ul>\'."\\n";
    }
    endif;
?>

SO网友:Harsh Barach

很容易创建分页链接,如First Previous..Next Last.

您应该在functions.php.

<?php
function pagination($pages = \'\', $range = 4)
{  
     $showItems = ($range * 2)+1;  

     global $paged;
     if(empty($paged)) $paged = 1;

     if($pages == \'\')
     {
         global $wp_query;
         $pages = $wp_query->max_num_pages;
         if(!$pages)
         {
             $pages = 1;
         }
     }   

     if(1 != $pages)
     {

         if($paged > 2 && $paged > $range+1 && $showItems < $pages) echo "<a href=\'".get_pagenum_link(1)."\'>&laquo; First</a>";
         if($paged > 1 && $showItems < $pages) echo "<a href=\'".get_pagenum_link($paged - 1)."\'>&lsaquo; Previous</a>";

         for ($i=1; $i <= $pages; $i++)
         {
             if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showItems ))
             {
                 echo ($paged == $i)? "<span class=\\"current\\">".$i."</span>":"<a href=\'".get_pagenum_link($i)."\' class=\\"inactive\\">".$i."</a>";
             }
         }

         if ($paged < $pages && $showItems < $pages) echo "<a href=\\"".get_pagenum_link($paged + 1)."\\">Next &rsaquo;</a>";  
         if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showItems < $pages) echo "<a href=\'".get_pagenum_link($pages)."\'>Last &raquo;</a>";
         echo "</div>\\n";
     }
}
?>
您可以通过放置下面的代码从各自的模板调用。

<?php
    if (function_exists("pagination"))
    {
        pagination($additional_loop->max_num_pages);
    }
?>

SO网友:Kohlo

我们找到了一个有点简单的修复方法,虽然与最初的想法相比并不是百分之百的完美,但在没有找到让代码工作得让我抓狂的方法的情况下,这项工作做得足够好。我会把它们贴在这里,以防其他人遇到这种情况,并希望在没有专用插件的情况下快速而肮脏地在Wordpress上获得漫画导航。

对于单个岗位:<div class="comicsnav"> <a href="STATIC-URL-TO-FIRST-POST"><i class="fa fa-angle-left"></i><i class="fa fa-angle-left"></i> First</a> | <?php previous_post_link(\'%link\', \'<i class="fa fa-angle-left"></i> Prev\'); ?> | <?php next_post_link(\'%link\', \'Next <i class="fa fa-angle-right"></i>\'); ?> | <a href="STATIC-URL-TO-MAIN-PAGE">Last <i class="fa fa-angle-right"></i><i class="fa fa-angle-right"></i></a> <?php wp_get_recent_posts(); ?> </div>

对于主页:<div class="comicsnav"> <a href="STATIC-URL-TO-FIRST-POST"><i class="fa fa-angle-left"></i><i class="fa fa-angle-left"></i> First</a> | <?php previous_post_link(\'%link\', \'<i class="fa fa-angle-left"></i> Prev\'); ?> | Current | <a href="STATIC-URL-TO-MAIN-PAGE">Last <i class="fa fa-angle-right"></i><i class="fa fa-angle-right"></i></a> <?php wp_get_recent_posts(); ?> </div>

我们在这里遇到的一个障碍是,如果你在第一篇或最后一篇文章中,它会显示两个相邻的管道,因为那里没有显示上一个/下一个链接,这是我目前可以接受的。如果您对此感到不安,管道是可选的,因此可以将其完全删除,以显示导航为(<<;第一个(<);上一个下一个>上一个>>

结束

相关推荐

Plain permalinks not working!

对于我的wordpress网站,普通永久链接不起作用。帖子url正在更改,但当我们单击帖子时,它不会重定向到帖子页面。它将出现在主页上。例如:http://example.com/?p=14523 如果我将永久链接更改为其他自定义格式,它将正常工作并显示帖子页面。例如:http://example.com/2018/09/01/postname/ 我尝试创建一个新的.htaccess 将永久链接更改为普通后的文件。仍然不工作。