爱玺玺

爱玺玺的生活日记本。wx:lb87626

worldpress文章列表分页

<?php

get_header();

?>

<div id="lists">

    <?php

    // 分页参数

    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;


    // 设置查询参数

    $args = array(

        'posts_per_page' => 4, // 每页显示的文章数

        'paged' => $paged, // 当前页码

    );


    // 执行查询

    $query = new WP_Query($args);


    // 检查是否有文章

    if ($query->have_posts()) :

        // 开始循环

        while ($query->have_posts()) : $query->the_post();

            ?>

            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br>

        <?php

        endwhile;


        // 输出分页链接

        $total_pages = $query->max_num_pages;

        $current_page = max(1, get_query_var('paged'));

        $big = 999999999; // 这是一个大数字,用于确保分页链接中的页码参数不会与WordPress的其他查询参数冲突


        echo paginate_links(array(

            'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),

            'format' => '?paged=%#%',

            'current' => $current_page,

            'total' => $total_pages,

            'prev_text' => '&laquo;',

            'next_text' => '&raquo;',

            'type' => 'plain',

        ));


        // 恢复全局$post对象

        wp_reset_postdata();

    else :

        // 没有文章时的处理

        echo '没有找到文章。';

    endif;

    ?>

</div>

<?php

get_footer();

?>


上面代码有点问题,改下面代码,下面代码需要worldpress配置为默认,首页为调用文章列表,分页采用朴素模式

<?php

get_header();

?>

<div id="lists">

    <?php

    while (have_posts()){

        the_post();

        the_title('<a href="'.get_permalink().'">','</a>');

        echo '<br/>';

        //the_excerpt();//文章摘要

    }

    the_posts_pagination(array(

        'mid_size'           => 2,

        'prev_text'          => '上一页',

        'next_text'          => '下一页',

    ));

    ?>

</div>

<?php

get_footer();

?>




测试数据可以从 https://github.com/WordPress/theme-test-data

下载

image.png

用这个文件

image.png

可以用这个导入,导入里面的配置可以不填。


发表评论:

Powered By Z-BlogPHP 1.4 Deeplue Build 150101

Copyright Your WebSite.Some Rights Reserved.

蜀ICP备11021721号-5