设置为5时,WP\\u Query自定义查询在所有页面上仅显示相同的10篇文章。这是我的代码:
更新的代码:
<?php
global $wp_query;
$paged = (get_query_var(\'page\')) ? get_query_var(\'page\') : 1;
// WP_Query arguments
$args = array (\'pagination\' => true,
\'posts_per_page\' => 5,
\'ignore_stickie_posts\' => true,
\'page\' => $paged);
// The Query
$wp_query_new = new WP_Query( $args );
if ( $wp_query_new ->have_posts() ) {
while ( $wp_query_new ->have_posts() ) {
$wp_query_new ->the_post();
?> ...<?php
}
?>
<?php
$big = 999999999; // need an unlikely integer
$pages = paginate_links( array(
\'base\' => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
\'format\' => \'?page=%#%\',
\'current\' => max( 1, get_query_var(\'page\') ),
\'total\' => $wp_query_new ->max_num_pages,
\'type\' => \'array\',
) );
if( is_array( $pages ) ) {
$paged = ( get_query_var(\'page\') == 0 ) ? 1 : get_query_var(\'page\');
echo \'<ul class="pagination">\';
foreach ( $pages as $page ) {
echo "<li>$page</li>";
}
echo \'</ul>\';
}
?>
<?php } else {
?>
<p><?php _e(\'Not found, what you are looking for.\'); ?></p>
<?php }
// Restore original Post Data
wp_reset_postdata();
?>
在这方面有人能帮我吗?
SO网友:s_ha_dum
我能想到的两件事之一就是:
你的贴子出现了。尝试添加ignore_stickie_posts
论点
$args = array (
\'pagination\' => true,
\'posts_per_page\' => 5,
\'ignore_stickie_posts\' => true,
);
请参见:
https://wordpress.stackexchange.com/a/85658/21376你写得很差(太全球化了)pre_get_posts
滤器类似于:
function naughty_pre_get_posts( $query ) {
$query->set( \'posts_per_page\', 1 );
}
add_action( \'pre_get_posts\', \'naughty_pre_get_posts\' );
还有,当您命名查询时
$wp_query
您对页面上的主查询进行了重击,这可能会导致意外/不可预测的影响。为自定义查询选择一个新名称,或者如果您试图正确更改主查询,请将整个查询转换为
pre_get_posts
滤器