【问题标题】:Paginate posts in WordPress Theme在 WordPress 主题中为帖子分页
【发布时间】:2011-08-17 09:35:56
【问题描述】:

好的,我已经盯着这个看了将近一个小时,但无法让它工作。我正在使用 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 循环抓取列表中的第一个帖子并将其显示在自己的特殊块中。第二个 while 循环抓取剩余的 4 个帖子。这两个 while 循环都可以正常工作。分页只是没有打印出来。

【问题讨论】:

标签: php wordpress pagination


【解决方案1】:

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

【讨论】:

  • 今天我会试一试,告诉你进展如何!
  • 还是什么都没有...我什至尝试在前面添加一个“回声”。它不会打印任何内容。
【解决方案2】:

找到我的解决方案的答案。如果您将“总数”保留为 1,则该函数不执行任何操作。您必须将页面数量放入“total”参数中。我认为 WordPress 会足够聪明,可以让该功能从集合中获取帖子的数量......

无论如何 - 任何其他正在寻找不需要大量自定义代码的分页功能(如 paginate_links 需要)的人,请查看以下内容: http://wordpress.org/extend/plugins/wp-paginate/

似乎工作得很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-20
    • 1970-01-01
    • 2014-12-28
    • 1970-01-01
    • 1970-01-01
    • 2013-01-23
    • 2022-06-12
    相关资源
    最近更新 更多