1

嗨有一个问题,我无法在静态首页上进行分页,查看了 wordpress codex 并完成了它所说的构建但仍然没有乐趣,

非常感谢任何帮助

<?php
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$type = 'Galleries';
$args=array(
   'category_name' => 'Gallery',
   'post_status' => 'publish',
   'posts_per_page' => 2,
   'paged' => $paged 
 );

$my_query = null;
$my_query = new WP_Query($args);
?> 
<section id="maincontent" class="twelve columns">
<ul id="gallery_menu">
<?php if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <li class="gallery_titleframe">
            <div class="gallery_titleborder">
                <div class="gallery_titleimage">
                <?php $str = get_the_ID() ; ?> 
                    <a href="<?php the_permalink() ?>" title="<?php  echo get_post_meta($str, 'gallery1', true); ?>">  
                        <img src="<?php echo get_post_meta($str, 'gallery1', true); ?>"  width="256" height="186">
                     </a>
                </div>
            </div>
            <div class="gallery_titletext">
                <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
            </div>
         </li>    
<?php endwhile;
    } ?> 
        <div id="nav-below" class="navigation">
            <div class="nav-previous"><?php next_posts_link(); ?></div>
            <div class="nav-next"><?php previous_posts_link(); ?></div>
        </div><!-- #nav-below -->

 <?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>
4

2 回答 2

2

next_posts_linkprevious_posts_link依赖于全局$wp_query。您可以在调用它们之前将您的自定义查询临时分配给全局。

<?php
  $temp = $wp_query;
  $wp_query = null;
  $wp_query = $my_query;
?>
<div id="nav-below" class="navigation">
  <div class="nav-previous"><?php next_posts_link(); ?></div>
  <div class="nav-next"><?php previous_posts_link(); ?></div>
</div><!-- #nav-below -->
<?php
  $wp_query = null;
  $wp_query = $temp;
  wp_reset_query();
?>
于 2012-11-21T15:27:21.670 回答
1

替换这个

$paged = (get_query_var('page')) ? get_query_var('page') : 1;

有了这个

if ( get_query_var('paged') ) {
    $paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
    $paged = get_query_var('page');
} else {
    $paged = 1;
}
于 2013-05-15T05:25:56.767 回答