分页在自定义循环WP_QUERY中不起作用

时间:2017-10-28 作者:Vova

请帮助分页

我有很好的代码(functions.php)

function projects_custom_number_of_posts( $query ) {
    $query->set( \'posts_per_page\', 2 );
    return;
}
add_action( \'pre_get_posts\', \'projects_custom_number_of_posts\');
当我更改为时,分页停止工作

function projects_custom_number_of_posts( $query ) {
    $postsperpage = $query->get( \'posts_per_page\' );
    $query->set( \'posts_per_page\', $postsperpage );
    return;
}
add_action( \'pre_get_posts\', \'projects_custom_number_of_posts\');
数据必须取自\'posts_per_page\'

$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$args = array(\'product_cat\' => $cat, \'post_type\' => \'product\',  \'posts_per_page\' => 2, \'paged\' => $paged ); $query = new WP_Query( $args );

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

1 个回复
SO网友:user2462948

有同样的问题。以以下代码结束

global $woocommerce_loop;
$woocommerce_loop[\'paged\'] = (isset($woocommerce_loop[\'paged\'])) ? $woocommerce_loop[\'paged\'] : 1;
$woocommerce_loop[\'total\'] = $custom_query->found_posts;
$woocommerce_loop[\'post_count\'] = $custom_query->post_count;
$woocommerce_loop[\'per_page\'] = $per_page;
$woocommerce_loop[\'total_pages\'] = ceil($custom_query->found_posts/$per_page);
$woocommerce_loop[\'current_page\'] = $args[\'paged\'];

结束

相关推荐

Custom taxonomy pagination

我正在为自定义分类法存档页创建自定义编号分页。因此,我在显示分页、显示正确的链接等方面没有问题。但是,当我单击第2页上方的链接时(例如第3页,如:my-website.com/page/3/?my_category=some-term我得到404-页面不存在。该类别包含60多篇帖子,应该有8页,但只有第一页和第二页正确显示my-website.com/?my_category=some-term和my-website.com/page/2/?my_category=some-term但以上所有的都是404