【问题标题】:Wordpress Pagination in Custom theme自定义主题中的 Wordpress 分页
【发布时间】:2013-08-20 08:33:50
【问题描述】:

我正在使用以下代码在我的 wordpress 页面上生成分页:

<?php
    global $wp_query;

    $big = 999999999; // need an unlikely integer

    echo paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $wp_query->max_num_pages
    ) );
?>

通知'format' =&gt; '?paged=%#%'。根据 Codex,漂亮的链接会有不同的格式,即codex says

格式 (字符串)(可选)用于分页结构。默认值为'?page=%#%',如果使用漂亮的永久链接,则为'/page/%#%',其中'%#%' 将替换为页码。 默认值:'?page=%#%'

我得到的是,每当我更改永久链接格式时,我都必须更改主题文件中的 php 代码。那会非常乏味,所以有什么方法可以让我的分页适应永久链接样式,即如果我将永久链接样式更改为漂亮,它不会中断?

【问题讨论】:

    标签: wordpress wordpress-theming


    【解决方案1】:

    我在 Wordpress Codex here 中挖掘了一些东西

    using_mod_rewrite_permalinks - 返回 true 您的博客正在通过 mod_rewrite 使用“漂亮”的永久链接。

    所以试试这个

    <?php
    global $wp_query;
    global $wp_rewrite;
    
    $big = 999999999; // need an unlikely integer
    
    if( $wp_rewrite->using_mod_rewrite_permalinks() ) {
        myformat = '/page/%#%';
    } else {
        myformat = '?page=%#%';
    }
    
    echo paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => myformat,
        'current' => max( 1, get_query_var('paged') ),
        'total' => $wp_query->max_num_pages
    ) );
    ?>
    

    【讨论】:

      猜你喜欢
      • 2017-03-19
      • 2018-09-23
      • 1970-01-01
      • 1970-01-01
      • 2021-02-07
      • 1970-01-01
      • 1970-01-01
      • 2015-03-09
      相关资源
      最近更新 更多