行业新闻

    wordpress 自动分页wordpress中自定义查询后的分页

    2022-06-04    来源:www.8001717.cn

    主循环会根据url中的page参数自动对数据进行分页,但是如果你的page值超过了主循环持有的数据量,就会被定义到404页面。写在自己页面上的代码也不会被加载。

    如果你想让你的页面接收到page参数,你必须在加载404页面之前跳转到你自己的页面。

    在-.php中,在加载模板之前会有一个过滤器的行为,这个我们可以做文章。下面是-.php中的几行代码,看看就好wordpress 自动分页wordpress做网站,不要改。

    	if ( $template = apply_filters( 'template_include', $template ) )
    		include( $template );
    	return;

    在自己主题的.php中,添加一个函数''"(这里是我定义的函数名,你可以随意设置)wordpress做网站,代码如下:

    add_filter('template_include','change404ToCategory');
    function change404ToCategory($template){//exit();
    	global $wp_query;
    	$wp_query->is_category = true;
    	$wp_query->is_404 = false;
    	if(is_category('xxxx')){
                $template = get_category_template();
    	}
    	return $template;
    }

    我要定义页面,所以我把$改成true(因为之前没有查询到posts数据wordpress 自动分页,所以定义为false);改为false(如果为true,在获取body的class时会调用添加a); (xxxx) 我只需要在xxxx中使用自定义分页。

    $ = e();

    这句话是获取分页模板。其他模板请参考-.php;

    终于。

    添加到你自己的 -xxxx

    	global $paged;
    	query_posts(array( paged'=>$paged,"showposts" => 20));

    $paged 是您过去传递的页码。传过来的时候是page,但是wp会改成paged。查询时,paged对应的$paged对应每页显示的文章数,加上需要其他参数,即可完成自定义分页。当然,分页css需要你自己写。

    文章来自互联网,侵权请联系删除,文章阐述观点来自文章出处,并不代表本站观点。 www.8001717.cn



X