You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress剧院票务网站查询短码求助:分页等功能实现问题

嘿,我来帮你排查这个短码分页失效的问题!作为新手踩这个坑太正常啦,咱们一步步来捋清楚问题出在哪:

一、先搞定$paged变量的正确获取

你代码里用了get_query_var('paged'),但在短码场景下,这个全局变量经常会“失灵”——比如短码放在静态页面、自定义模板里时,WordPress没法正确识别当前页码。给你两个更稳妥的写法:
要么针对页面场景调整:

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

要么直接从URL参数里安全获取:

$paged = isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;

(absint是为了过滤恶意参数,保证页码是正整数,安全第一~)

二、给WP_Query补上关键的paged参数

你目前的$args数组里没传paged参数!这是分页失效的核心原因——WP_Query根本不知道要加载第几页的内容,自然不会生成分页逻辑。赶紧把这个参数加上:

$args = array(
    'category_name' => 'teatro',
    'paged' => $paged, // 就是这行!必须加
    'posts_per_page' => 6, // 这里可以设置你想要的每页文章数
    // 其他你需要的参数
);
三、短码里输出分页的正确姿势

你用了ob_start()来捕获输出,那循环结束后,不能直接用默认的the_posts_pagination()(它默认绑定全局的$wp_query,不认你的自定义查询),得用paginate_links()指定你的查询对象的总页数。举个完整的例子:

function test_posts() {
    ob_start();
    // 正确获取页码
    $paged = isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
    
    $args = array(
        'category_name' => 'teatro',
        'paged' => $paged,
        'posts_per_page' => 6,
    );
    $the_query = new WP_Query( $args );

    // 循环输出文章
    if ( $the_query->have_posts() ) {
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            // 这里写你的文章输出逻辑,比如标题、内容之类的
            the_title( '<h2>', '</h2>' );
            the_excerpt();
        }

        // 输出分页
        echo paginate_links( array(
            'total' => $the_query->max_num_pages, // 总页数
            'current' => $paged, // 当前页码
            'format' => '?paged=%#%', // URL的页码参数格式
            'prev_text' => '← 上一页',
            'next_text' => '下一页 →',
        ) );

        // 别忘了重置查询,避免影响全局循环
        wp_reset_postdata();
    } else {
        echo '暂无文章';
    }

    return ob_get_clean();
}
add_shortcode( 'test_posts', 'test_posts' );
四、扩展你需要的额外功能

如果你要实现分页开关、分类选择、每页数量设置,可以给短码加自定义属性,让它更灵活:

function test_posts( $atts ) {
    // 设置默认属性
    $atts = shortcode_atts( array(
        'pagination' => 'on', // 默认开启分页
        'category' => 'teatro', // 默认分类
        'posts_per_page' => 6, // 默认每页数量
    ), $atts );

    ob_start();
    $paged = isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
    
    $args = array(
        'category_name' => $atts['category'],
        'paged' => $paged,
        'posts_per_page' => absint( $atts['posts_per_page'] ),
    );
    $the_query = new WP_Query( $args );

    // 文章循环逻辑...

    // 根据开关决定是否输出分页
    if ( $atts['pagination'] === 'on' && $the_query->max_num_pages > 1 ) {
        echo paginate_links( array(
            'total' => $the_query->max_num_pages,
            'current' => $paged,
            'format' => '?paged=%#%',
            'prev_text' => '← 上一页',
            'next_text' => '下一页 →',
        ) );
    }

    wp_reset_postdata();
    return ob_get_clean();
}
add_shortcode( 'test_posts', 'test_posts' );

之后就可以这样调用短码:[test_posts pagination="off" category="drama" posts_per_page="8"]

这样调整后,你的分页功能应该就能正常工作啦!

内容的提问来源于stack exchange,提问作者Daniel Watson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:54:12