如何实现 Wordpress 分页? [英] How to implement Wordpress pagination?

查看:34
本文介绍了如何实现 Wordpress 分页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的帖子实现分页.虽然我对 php 函数以及如何调用它有点困惑.

I'm trying to implement pagination for my posts. Though I'm a bit stuck on the php function and also how to call it.

我猜最简单的方法是:

<?php posts_nav_link(); ?>

但是如果我想要自定义分页怎么办?

But what if I want custom pagination?

这是我当前的代码:

<?php 
    global $wp_query;
    $total = $wp_query->max_num_pages;
    if ( $total > 1 )  {
         if ( !$current_page = get_query_var('paged') )
              $current_page = 1;
         echo paginate_links(array(
              'base' => get_pagenum_link(1) . '%_%',
              'format' => '?paged=%#%',
              'current' => $current_page,
              'total' => $total,
              'mid_size' => 4,
              'type' => 'list'
         ));
    }
?>

这是正确的,我该如何称呼它?在 index.php 中?循环中的哪个位置?谢谢.

Is this correct and how do I call it? In index.php? Where in the loop? Thanks.

编辑澄清:如何实现此代码?目前我已经把它放在我的functions.php 中.那么我如何(以及在​​循环中的哪个位置)引用"这个函数以便显示分页.

Edit for clarification: How do I implement this code? At the moment I have put it in my functions.php. So how (and where in the loop) do I 'reference' this function so the pagination is displayed.

推荐答案

有两种方法可以实现此代码.看起来你现在介于两者之间.

There are two ways you can implement this code. It looks like right now you are sort of in between the two.

第一种方法是将您的分页代码直接添加到模板中,它将在循环内的某处使用(最有可能在结束 <?php endwhile; ?> 标签).如果你使用的是 single.php 模板,你会把它放在那里,如果没有,把它放在 index.php 中.将它放置在循环内取决于您希望分页出现在页面上的位置.

The first way would be to add your pagination code directly into the template that it will be used in somewhere inside the loop (most likely somewhere right before the closing <?php endwhile; ?> tag). If you are using a single.php template, you would put it in there, if not, put it in index.php. The placing of it inside of the loop depends on where you want the pagination to appear on your page.

第二种方法是将分页代码添加到functions.php文件中(你已经完成了).但是,您需要为此稍微修改代码.您需要将代码包装在一个函数中,并为该函数命名.我以 your_custom_pagination 为例.您的functions.php 文件很可能已经包含在php 标签中,因此我已将其删除.

The second way is to add the pagination code to the functions.php file (which you have done). However, you will need to revise your code a bit for this. You need to wrap the code within a function, and name that function something. I've used your_custom_pagination for an example. Your functions.php file is most likely already wrapped in php tags, so I have removed those.

function your_custom_pagination() {
    global $wp_query;
    $total = $wp_query->max_num_pages;
    if ( $total > 1 ) {
        if ( !$current_page = get_query_var('paged') ) {
            $current_page = 1;
        }
        echo paginate_links(array(
            'base' => get_pagenum_link(1) . '%_%',
            'format' => '?paged=%#%',
            'current' => $current_page,
            'total' => $total,
            'mid_size' => 4,
            'type' => 'list'
        ));
    }
}

然后您需要进入您正在使用的模板并放置此代码 <?php your_custom_pagination();?> 到我上面说明的同一个地方来调用分页函数.

Then you'll need to go into the template that you are using and place this code <?php your_custom_pagination(); ?> into the same spot I illustrated above to call the pagination function.

我还没有真正测试过你的代码,所以假设它是有效的,一切都应该可以工作.

I haven't actually tested your code, so assuming it's valid everything should work.

这篇关于如何实现 Wordpress 分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆