我一直在努力获取索引的主循环。php可以正确使用WP\\U查询函数。我需要在顶部的6个特色图像网格阵列不被抵消,而抵消了6个职位下面的主要职位循环。
任何涉及query\\u posts()的操作都不可避免地会破坏分页。使用单个循环可以修复分页问题,但不希望将偏移应用到特征网格部分。我已经使用WordPress codex推荐的pre\\u get\\u posts修复程序成功地实现了分页主循环https://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination.但是,此修复程序也会将特色帖子循环设置覆盖6。
对于WordPress开发人员来说,偏移/分页困境似乎是一个众所周知的问题,我已经浏览了几十篇文章、论坛和教程,但我找不到太多关于pre\\u get\\u posts修复覆盖索引上其他自定义循环设置时该怎么办的文档。pre\\u get\\u帖子在一定程度上起作用,我只是希望格式只适用于主页上的主帖子循环,而不是前6篇帖子。
这是代码。感谢您抽出时间阅读本文。
指数php:
<?php get_header();
do_shortcode( \'[mg-masonry_js]\' );
$options = get_option(\'mg_options\');
$option_cats = $options[\'mg-posts-categories\'];
$option_pg_number = $options[\'mg-posts-post-per-page\'];
$args = array(
\'post_type\' => \'post\',
\'post_status\' => array( \'publish\' ),
\'posts_per_page\' => $option_pg_number,
\'orderby\' => \'date\',
\'order\' => \'DESC\',
\'cat\' => $option_cats,
\'offset\' => 0,
);
?>
<div id="all-content-wrapper">
<div id="home-content" >
<div id="grid">
<?php if ($paged < 1 ) {
$paged = get_query_var(\'paged\'); ?>
<?php // initialize main loop ?>
<?php $grid_query = new WP_Query( $args );
if ( $grid_query->have_posts() ) :
while ( $grid_query->have_posts()) : $grid_query->the_post();
?>
<?php switch ( $grid_query->current_post ) {
case 0 :
?>
<?php // First featured image code here ?>
<?php echo get_template_part(\'grid_featured\'); ?>
<?php break; ?>
<?php // Grid image array code here ?>
<?php default : ?>
<?php echo get_template_part(\'grid\'); ?>
<?php } ?> <?php // end switch ?>
<?php endwhile;
wp_reset_postdata();
rewind_posts();
else : ?>
<h2><?php _e(\'No posts.\', \'news portal\' ); ?></h2>
<br><br><p class="lead"><?php _e(\'No posts to display.\', \'news portal\' ); ?></p>
<?php endif; ?>
<?php } // end paged switch ?>
</div> <?php // end grid ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
$args = array(
\'post_type\' => \'post\',
\'posts_per_page\' => 10,
\'offset\' => 6,
);
?>
<?/* Start the Loop */?>
<? $main_query = new WP_Query( $args );
if ( have_posts() ) :
while ( have_posts() ) : the_post();
get_template_part( \'template-parts/blog\', get_post_format() );
endwhile;
echo regular_pagination();
else :
get_template_part( \'template-parts/content\', \'none\' );
endif; ?>
</main><!-- #main -->
</div><!-- #primary -->
<?php news_portal_get_sidebar();?>
</div><!-- #all_content_wrapper -->
<?php get_footer();?>
功能。php:
/*--Offset Pre_Get_Posts pagination fix--*/
add_action(\'pre_get_posts\', \'myprefix_query_offset\', 1 );
function myprefix_query_offset(&$query) {
if ( ! $query->is_home() ) {
return;
}
$offset = 6;
$ppp = get_option(\'posts_per_page\');
if ( $query->is_paged ) {
$page_offset = $offset + ( ($query->query_vars[\'paged\']-1) * $ppp );
$query->set(\'offset\', $page_offset );
}
else {
$query->set(\'offset\',$offset);
}
}
add_filter(\'found_posts\', \'myprefix_adjust_offset_pagination\', 1, 2 );
function myprefix_adjust_offset_pagination($found_posts, $query) {
$offset = 6;
if ( $query->is_home() ) {
return $found_posts - $offset;
}
return $found_posts;
}