簡體   English   中英

WordPress 主題中的帖子分頁

[英]Paginate posts in WordPress Theme

好吧,我已經盯着這個看了將近一個小時,無法讓它工作。 我正在使用 WordPress,我在系統中有 12 個帖子。 在管理員中,我將其設置為每頁顯示 5 個帖子。 在我的主題中,我試圖在底部顯示分頁 (prev,1,2,3,4,next)。 我在 WordPress 指南上找到了 paginate_links() function 並且它沒有打印出任何內容...感謝您的幫助:

<?php get_header(); ?>
<div class="middle-container">
    <div class="middle">
        <div class="column main">
            <div class="latest">
            <?php
            $i = 0;
            if (have_posts()):
                while (have_posts() && $i < 1):
                    the_post();
                    ?>
                    <div class="image"><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_post_thumbnail(); ?></a>
                    </div>
                    <div class="content">
                    <h1><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
                    <span class="date"><?php the_time('F jS, Y') ?></span>
                    <?php
                    the_excerpt();
                    ?>
                    </div>
                    <?php
                    $i++;
                endwhile;
            endif;
            ?>
            </div>
            <ul class="posts">
            <?php
            $i = 0;
            if (have_posts()):
                while (have_posts() && $i < 4):
                    the_post();
                    ?>
                    <li>
                    <h2><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
                    <span class="date"><?php the_time('F jS, Y') ?></span>
                    <?php
                    the_excerpt();
                    ?>
                    </li>
                    <?php
                    $i++;
                endwhile;
            endif;
            ?>
            </ul>
            <?php
            echo paginate_links();
            ?>
        </div>
        <div class="column sidebar"></div>
    </div>
</div>
<?php get_footer(); ?>

更多背景知識:代碼中的第一個 while 循環抓取列表中的第一個帖子並將其顯示在自己的特殊塊中。 第二個 while 循環抓取剩余的 4 個帖子。 這兩個 while 循環都可以正常工作。 分頁只是沒有打印出來。

Wordpress paginate_links function 似乎需要一個參數。

我會嘗試他們的默認示例,看看它是否有效。

<?php
paginate_links(array(
    'base'         => '%_%',
    'format'       => '?page=%#%',
    'total'        => 1,
    'current'      => 0,
    'show_all'     => False,
    'end_size'     => 1,
    'mid_size'     => 2,
    'prev_next'    => True,
    'prev_text'    => __('&laquo; Previous'),
    'next_text'    => __('Next &raquo;'),
    'type'         => 'plain',
    'add_args'     => False,
    'add_fragment' =>  ));
?>

找到了我的解決方案的答案。 如果將“總數”保留為 1,則 function 什么也不做。 您必須將頁面數量放入“total”參數中。 我認為 WordPress 足夠聰明,可以讓 function 從集合中獲取帖子數量......

無論如何 - 任何其他正在尋找不需要大量自定義代碼的分頁功能(如 paginate_links )的人,請查看: http://wordpress.org/extend/plugins/wp-paginate/

似乎工作得很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM