我正在开发一个自定义wordpress主题。在我的主页中,我创建了一个自定义循环,以在网格视图中显示最新帖子。。我正在尝试在那里添加分页,分页也会显示出来。但是分页每次都加载相同的Post网格。这个循环还反向添加了我想在网格后的某个位置添加广告网格的位置。。
这是网格链接https://dev.ahsanurrahman.com/myprojects/cms/wp/csdug/
我的代码如下,我正在使用索引。php作为首页显示此帖子网格…
<?php
/**
* The main template file
*/
get_header(); ?>
<?php // Advertisement Placeing Number Functions ?>
<?php if ( function_exists( \'ot_get_option\' ) ) {$place_of_first_add_in_home_page = ot_get_option( \'place_of_first_add_in_home_page\', \'\' );} ?>
<?php if ( function_exists( \'ot_get_option\' ) ) {$place_of_second_add_in_home_page = ot_get_option( \'place_of_second_add_in_home_page\', \'\' );} ?>
<?php if ( function_exists( \'ot_get_option\' ) ) {$place_of_third_add_in_home_page = ot_get_option( \'place_of_third_add_in_home_page\', \'\' );} ?>
<?php if ( function_exists( \'ot_get_option\' ) ) {$place_of_fourth_add_in_home_page = ot_get_option( \'place_of_fourth_add_in_home_page\', \'\' );} ?>
<?php if ( function_exists( \'ot_get_option\' ) ) {$place_of_fifth_add_in_home_page = ot_get_option( \'place_of_fifth_add_in_home_page\', \'\' );} ?>
<?php // Advertisement Block Functions ?>
<?php if ( function_exists( \'ot_get_option\' ) ) {$p300x250__1__home = ot_get_option( \'p300x250__1__home\', \'\' );} ?>
<?php if ( function_exists( \'ot_get_option\' ) ) {$p300x250__2__home = ot_get_option( \'p300x250__2__home\', \'\' );} ?>
<?php if ( function_exists( \'ot_get_option\' ) ) {$p300x250__3__home = ot_get_option( \'p300x250__3__home\', \'\' );} ?>
<?php if ( function_exists( \'ot_get_option\' ) ) {$p300x250__4__home = ot_get_option( \'p300x250__4__home\', \'\' );} ?>
<?php if ( function_exists( \'ot_get_option\' ) ) {$p300x250__5__home = ot_get_option( \'p300x250__5__home\', \'\' );} ?>
<?php // Advertisement Block Functions ?>
<div class="main-content">
<div class="main-wrap">
<div class="col-md-12 hero-post">
<?php query_posts(\'order=dsc&showposts=1\'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="col-md-12">
<div class="col-md-9">
<div class="hero_post_thumbnail">
<a href="<?php the_permalink();?>"><?php if ( ! post_password_required() && ! is_attachment() ) :
the_post_thumbnail();
endif;
?></a>
</div><!-- post thumbnail ends here -->
</div><!-- col-md-9 ends here -->
<div class="col-md-3">
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<h5 class="csdug_subtitle"><?php global $post; echo get_post_meta($post->ID,\'wpshed_textfield\',true) ?></h5>
<h3 class="entry-title"><?php the_title(); ?></h3></a>
<div class="home-grid-post-info"><?php csdug_entry_meta(); ?></div>
<div class="home-para"><?php the_excerpt();?></div>
<div class="post_love_count"><?php echo do_shortcode(\'[post_love_count]\');?></div>
<div class="clr"></div>
<?php echo do_shortcode(\'[social_share_icons]\');?>
<div class="clr"></div>
</div><!-- col-md-3 ends here -->
</div><!-- col-md-12 ends here -->
<?php endwhile; ?>
<?php endif; ?>
</div><!-- hero post ends here -->
<div class="col-md-12">
<?php
$paged = ( get_query_var(\'page\') ) ? get_query_var(\'page\') : 1;
$query_args = array(
\'post_type\' => \'post\',
\'posts_per_page\' => 3,
\'paged\' => $paged,
\'page\' => $paged,
\'offset\' => 1
);
$the_query = new WP_Query( $query_args ); ?>
<?php $adcounter = 1; ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<?php if ($adcounter == $place_of_first_add_in_home_page) : ?>
<?php {?>
<div class="col-md-4 home-post-grid post-container">
<?php echo $p300x250__1__home;?>
</div><!-- advertisement area ends here -->
<div class="col-md-4 home-post-grid">
<div class="other_post_thumbnail">
<a href="<?php the_permalink();?>"><?php if ( ! post_password_required() && ! is_attachment() ) :
the_post_thumbnail();
endif;
?></a>
</div><!-- post thumbnail ends here -->
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<h5 class="csdug_subtitle"><?php global $post; echo get_post_meta($post->ID,\'wpshed_textfield\',true) ?></h5>
<h3 class="entry-title"><?php the_title(); ?></h3></a>
<div class="home-grid-post-info"><?php csdug_entry_meta(); ?></div>
<div class="home-para"><?php the_excerpt();?></div>
<div class="post_love_count"><?php echo do_shortcode(\'[post_love_count]\');?></div>
<div class="clr"></div>
<?php echo do_shortcode(\'[social_share_icons]\');?>
<div class="clr"></div>
</div><!-- home post grid ends here -->
<?php }?>
<?php elseif ($adcounter == $place_of_second_add_in_home_page) :{ ?>
<div class="col-md-4 home-post-grid">
<?php echo $p300x250__2__home;?>
</div><!-- advertisement area ends here -->
<div class="col-md-4 home-post-grid">
<div class="other_post_thumbnail">
<a href="<?php the_permalink();?>"><?php if ( ! post_password_required() && ! is_attachment() ) :
the_post_thumbnail();
endif;
?></a>
</div><!-- post thumbnail ends here -->
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<h5 class="csdug_subtitle"><?php global $post; echo get_post_meta($post->ID,\'wpshed_textfield\',true) ?></h5>
<h3 class="entry-title"><?php the_title(); ?></h3></a>
<div class="home-grid-post-info"><?php csdug_entry_meta(); ?></div>
<div class="home-para"><?php the_excerpt();?></div>
<div class="post_love_count"><?php echo do_shortcode(\'[post_love_count]\');?></div>
<div class="clr"></div>
<?php echo do_shortcode(\'[social_share_icons]\');?>
<div class="clr"></div>
</div><!-- home post grid ends here -->
<?php } ?>
<?php // Second Else if ends here ?>
<?php elseif ($adcounter == $place_of_third_add_in_home_page) :{ ?>
<div class="col-md-4 home-post-grid">
<?php echo $p300x250__3__home;?>
</div><!-- advertisement area ends here -->
<div class="col-md-4 home-post-grid">
<div class="other_post_thumbnail">
<a href="<?php the_permalink();?>"><?php if ( ! post_password_required() && ! is_attachment() ) :
the_post_thumbnail();
endif;
?></a>
</div><!-- post thumbnail ends here -->
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<h5 class="csdug_subtitle"><?php global $post; echo get_post_meta($post->ID,\'wpshed_textfield\',true) ?></h5>
<h3 class="entry-title"><?php the_title(); ?></h3></a>
<div class="home-grid-post-info"><?php csdug_entry_meta(); ?></div>
<div class="home-para"><?php the_excerpt();?></div>
<div class="post_love_count"><?php echo do_shortcode(\'[post_love_count]\');?></div>
<div class="clr"></div>
<?php echo do_shortcode(\'[social_share_icons]\');?>
<div class="clr"></div>
</div><!-- home post grid ends here -->
<?php } ?>
<?php // 3rd Else if ends here ?>
<?php elseif ($adcounter == $place_of_fourth_add_in_home_page) :{ ?>
<div class="col-md-4 home-post-grid">
<?php echo $p300x250__4__home;?>
</div><!-- advertisement area ends here -->
<div class="col-md-4 home-post-grid">
<div class="other_post_thumbnail">
<a href="<?php the_permalink();?>"><?php if ( ! post_password_required() && ! is_attachment() ) :
the_post_thumbnail();
endif;
?></a>
</div><!-- post thumbnail ends here -->
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<h5 class="csdug_subtitle"><?php global $post; echo get_post_meta($post->ID,\'wpshed_textfield\',true) ?></h5>
<h3 class="entry-title"><?php the_title(); ?></h3></a>
<div class="home-grid-post-info"><?php csdug_entry_meta(); ?></div>
<div class="home-para"><?php the_excerpt();?></div>
<div class="post_love_count"><?php echo do_shortcode(\'[post_love_count]\');?></div>
<div class="clr"></div>
<?php echo do_shortcode(\'[social_share_icons]\');?>
<div class="clr"></div>
</div><!-- home post grid ends here -->
<?php } ?>
<?php // 4th Else if ends here ?>
<?php elseif ($adcounter == $place_of_fifth_add_in_home_page) :{ ?>
<div class="col-md-4 home-post-grid">
<?php echo $p300x250__5__home;?>
</div><!-- advertisement area ends here -->
<div class="col-md-4 home-post-grid">
<div class="other_post_thumbnail">
<a href="<?php the_permalink();?>"><?php if ( ! post_password_required() && ! is_attachment() ) :
the_post_thumbnail();
endif;
?></a>
</div><!-- post thumbnail ends here -->
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<h5 class="csdug_subtitle"><?php global $post; echo get_post_meta($post->ID,\'wpshed_textfield\',true) ?></h5>
<h3 class="entry-title"><?php the_title(); ?></h3></a>
<div class="home-grid-post-info"><?php csdug_entry_meta(); ?></div>
<div class="home-para"><?php the_excerpt();?></div>
<div class="post_love_count"><?php echo do_shortcode(\'[post_love_count]\');?></div>
<div class="clr"></div>
<?php echo do_shortcode(\'[social_share_icons]\');?>
<div class="clr"></div>
</div><!-- home post grid ends here -->
<?php } ?>
<?php // 5th Else if ends here ?>
<?php else : ?>
<?php {?>
<div class="col-md-4 home-post-grid">
<div class="other_post_thumbnail">
<a href="<?php the_permalink();?>"><?php if ( ! post_password_required() && ! is_attachment() ) :
the_post_thumbnail();
endif;
?></a>
</div><!-- post thumbnail ends here -->
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<h5 class="csdug_subtitle"><?php global $post; echo get_post_meta($post->ID,\'wpshed_textfield\',true) ?></h5>
<h3 class="entry-title"><?php the_title(); ?></h3></a>
<div class="home-grid-post-info"><?php csdug_entry_meta(); ?></div>
<div class="home-para"><?php the_excerpt();?></div>
<div class="post_love_count"><?php echo do_shortcode(\'[post_love_count]\');?></div>
<div class="clr"></div>
<?php echo do_shortcode(\'[social_share_icons]\');?>
<div class="clr"></div>
</div><!-- home post grid ends here -->
<?php }?>
<?php endif; ?>
<?php $adcounter++; ?>
<?php endwhile; ?>
</div> <!-- row ends here -->
<?php
if (function_exists(custom_pagination)) {
custom_pagination($the_query->max_num_pages,"",$paged);
}
?>
<?php wp_reset_postdata(); ?>
</div> <!-- col-md-12 ends here -->
<?php endif; ?>
</div><!-- main wrap ends here -->
<div class="clr"></div>
</div><!-- main content ends here -->
<?php get_footer(); ?>
下面是函数中的自定义分页代码。php
<?php // Custom Pagination for loop
function custom_pagination($numpages = \'\', $pagerange = \'\', $paged=\'\') {
if (empty($pagerange)) {
$pagerange = 2;
}
/**
* This first part of our function is a fallback
* for custom pagination inside a regular loop that
* uses the global $paged and global $wp_query variables.
*
* It\'s good because we can now override default pagination
* in our theme, and use this function in default quries
* and custom queries.
*/
global $paged;
if (empty($paged)) {
$paged = 1;
}
if ($numpages == \'\') {
global $wp_query;
$numpages = $wp_query->max_num_pages;
if(!$numpages) {
$numpages = 1;
}
}
/**
* We construct the pagination arguments to enter into our paginate_links
* function.
*/
$pagination_args = array(
\'base\' => get_pagenum_link(1) . \'%_%\',
\'format\' => \'page/%#%\',
\'total\' => $numpages,
\'current\' => $paged,
\'show_all\' => False,
\'end_size\' => 1,
\'mid_size\' => $pagerange,
\'prev_next\' => True,
\'prev_text\' => __(\'«\'),
\'next_text\' => __(\'»\'),
\'type\' => \'plain\',
\'add_args\' => false,
\'add_fragment\' => \'\'
);
$paginate_links = paginate_links($pagination_args);
if ($paginate_links) {
echo "<nav class=\'custom-pagination\'>";
echo "<span class=\'page-numbers page-num\'>Page " . $paged . " of " . $numpages . "</span> ";
echo $paginate_links;
echo "</nav>";
}
} ?>
最合适的回答,由SO网友:FluffyKitten 整理而成
在您的问题和评论之间,您说更改主查询的目的是从静态主页的主循环中排除最近的帖子,因为它在其他帖子之前单独显示为“英雄”帖子。
最简单的方法是使用主循环,并使用pre_get_posts
行动和found_posts
筛选以将其调整为删除最近的帖子,如下所示:
1. Index.php: Get the most recent post(s) using WP_Query
使用自定义查询获取最新帖子
<?php
// custom query to get the most recent post
$the_query = new WP_Query( array(\'posts_per_page\' => \'1\', \'order\' => \'DESC\') );
if( $the_query->have_posts() ):
while ( $the_query->have_posts() ) : $the_query->the_post();
global $post; ?>
[...display post content as required, e.g...]
<h1><a href="<?php the_permalink();?>"><?php the_title();?></a></h1>
<?php the_content(); ?>
<?php endwhile;
endif;
wp_reset_query(); // Restore global post data stomped by the_post().
?>
2. Functions.php: Set up offset and pagination to remove the most recent post(s) from the main loop钩入pre_get_posts
调整要跳过的帖子数量的查询和分页,以及found_posts
筛选以调整找到的帖子数的值。添加到函数。php:
/** Exclude the most recept post from the main query **/
add_action(\'pre_get_posts\', \'pre_query_homepage_offset\', 1 );
function pre_query_homepage_offset(&$query) {
// only continue if this is the main query on the homepage
if ( !$query->is_home() || !$query->is_main_query() )
return;
$posts_to_skip = 1; // the number of most recent posts to skip
$posts_per_page = get_option(\'posts_per_page\'); // get posts per page from WP settings
//Detect and handle pagination...
if ( !$query->is_paged ) // if we\'re on the first page...
$offset = $posts_to_skip; //...just offset by the number of posts to skip
else
// Calculate the page offset if we are not on the first page
$offset = $posts_to_skip + ( ($query->query_vars[\'paged\']-1) * $posts_per_page );
$query->set(\'offset\',$offset); // set the offset for the query
}
/** Adjust the number of posts found by the main query to account for our offset **/
add_filter(\'found_posts\', \'adjust_homepage_offset_pagination\', 1, 2 );
function adjust_homepage_offset_pagination($found_posts, $query) {
// return the actual value if this isn\'t the main query on the homepage
if ( !$query->is_home() || !$query->is_main_query() )
return $found_posts;
$posts_to_skip = 1; // the number of posts to skip
return $found_posts - $posts_to_skip;
}
3. Index.php: Display the rest of the posts (excluding the most recent) using the main loop现在我们可以使用主循环,而不是使用自定义查询,因为在步骤2中,我们的函数已经对其进行了调整,以排除最近的帖子。
<?php if ( have_posts() ) :
while ( have_posts() ) : the_post(); ?>
[...display post content as required e.g....]
<h2><?php the_title( ); ?></h2>
<?php the_content(); ?>
endwhile;
// Previous/next page navigation.
the_posts_pagination( array(
\'prev_text\' => __(\'«\'),
\'next_text\' => __(\'»\'),
) );
endif;
?>