php - 在 WordPress 主题中对帖子进行分页

标签 php wordpress pagination

好吧,我已经盯着这个看了将近一个小时了,但无法让它工作。我正在使用 WordPress,系统中有 12 个帖子。在管理员中,我将其设置为每页显示 5 个帖子。在我的主题中,我试图在底部显示分页 (prev,1,2,3,4,next)。我在 WordPress 指南上找到了 paginate_links() 函数,但它没有打印出任何内容……感谢任何帮助:

<?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 循环获取列表中的第一个帖子并将其显示在自己的特殊 block 中。第二个 while 循环获取剩余的 4 个帖子。这两个 while 循环都工作得很好。分页只是没有打印出来。

最佳答案

看起来 Wordpress paginate_links 函数需要一个参数。

我会尝试他们的默认示例,看看它是否有效。

<?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' =>  ));
?>

关于php - 在 WordPress 主题中对帖子进行分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5864578/

相关文章:

php - 在magento中仅加载特定 View 文件,没有页眉和页脚

javascript - 如何检测点击的元素是否不是 jQuery 元素的子元素?

php - 如何将此数组插入到我的 mysql php 表中?

java - 通过分页获取子数据

javascript - 在 Rails 中使用 dataTables 或过滤器和分页?

php - 分页链接无法正常工作 - 不正确的 PHP 函数?

javascript - 如何在复选框中组合相同命名的值并为其指定默认值

PHP - 变量名称作为数组键

php - 为什么删除错误抑制器后一行代码不能工作?

php - 有没有办法将 php 放在 IE 条件注释中