我正在开发一个自定义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>";
}
} ?>