Wordpress静态页面模板分页不起作用

时间:2016-04-04 11:35:10

标签: php wordpress static pagination

我正在构建自定义页面模板,而我似乎无法让分页工作。它一直显示与主页/首页相同的帖子。我尝试了很多不同的代码,但每个结果都遇到了同样的问题。

这是我正在使用的当前查询:

<?php 
global $paged;
global $wp_query;
$temp = $wp_query; 
$wp_query = null; 
$wp_query = new WP_Query(); 
$wp_query->query('posts_per_page=2&post_type=post'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post(); 
?>

<div class="news-item">
  <?php
  $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), '' );
  $url = $thumb['0'];
  ?>
  <div class="news-item-bg" style="background-image:url(<?=$url?>);"></div>

  <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
    <h2><?php the_title(); ?></h2></a>
    <div class="entry-meta">
      <div class="meta-date"><?php the_time('d.m.Y'); ?> </div><div class="meta-seperator"> | </div><div class="meta-author">Auteur: <?php the_author(); ?></div>
    </div><!-- .entry-meta -->
    <div class="excerpt"><?php the_excerpt(); ?></div>
  </div>

<?php endwhile; ?>

<?php previous_posts_link('&laquo; Newer') ?>
<?php next_posts_link('Older &raquo;') ?>

<?php 
$wp_query = null; 
$wp_query = $temp; 
?>

找到解决方案,此代码有效:

<?php
if ( get_query_var('paged') ) {
    $paged = get_query_var('paged');
} elseif ( get_query_var('page') ) { // 'page' is used instead of 'paged' on Static Front Page
$paged = get_query_var('page');
} else {
    $paged = 1;
}

$custom_query_args = array(
    'post_type' => 'post', 
    'posts_per_page' => '2',
    'paged' => $paged,
    'post_status' => 'publish',
    'ignore_sticky_posts' => true,
    //'category_name' => 'custom-cat',
    'order' => 'DESC', // 'ASC'
    'orderby' => 'date' // modified | title | name | ID | rand
    );
$custom_query = new WP_Query( $custom_query_args );

if ( $custom_query->have_posts() ) :
    while( $custom_query->have_posts() ) : $custom_query->the_post(); ?>


<div class="news-item">


    <?php

    if ( has_post_thumbnail() ) {
        $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), '' );
        $url = $thumb['0'];

        echo '<div class="news-item-bg" style="background-image:url(<?=$url?>);"></div>';         
    }
    else {

    }
    ;?>

    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
        <h2><?php the_title(); ?></h2></a>
        <div class="entry-meta">
            <div class="meta-date"><?php the_time('d.m.Y'); ?> </div><div class="meta-seperator"> | </div><div class="meta-author">Auteur: <?php the_author(); ?></div>
        </div><!-- .entry-meta -->
        <div class="excerpt"><?php the_excerpt(); ?></div>
        <div id="single-post-container"></div>
        <a class="button-1 load-more" href="<?php echo get_permalink(); ?>">Lees meer</a>
        <a class="ajax-close button-1" href="#">X</a>
    </div>

    <?php
    endwhile;
    ?>

    <?php if ($custom_query->max_num_pages > 1) : // custom pagination  ?>
        <?php
        $orig_query = $wp_query; // fix for pagination to work
        $wp_query = $custom_query;
        ?>
        <nav class="prev-next-posts">
            <div class="prev-posts-link">
                <?php echo get_next_posts_link( 'Older Entries', $custom_query->max_num_pages ); ?>
            </div>
            <div class="next-posts-link">
                <?php echo get_previous_posts_link( 'Newer Entries' ); ?>
            </div>
        </nav>
        <?php
        $wp_query = $orig_query; // fix for pagination to work
        ?>
    <?php endif; ?>

    <?php
    wp_reset_postdata(); // reset the query 
    else:
        echo '<p>'.__('Sorry, no posts matched your criteria.').'</p>';
    endif;
    ?>

1 个答案:

答案 0 :(得分:0)

尝试此代码只需复制pase

        <?php
        $args = array(
        'post_type'=> 'post',
        //'category_name'  => 'blog',
          'orderby' => 'post_date',
          //'posts_per_page'=>'1'
        'paged' => get_query_var('paged')
       );
        query_posts( $args ); 
        while (have_posts()) : the_post(); ?>

         <?php  the_title(); ?>

       <?php endwhile; ?>

<?php // Wordpress Pagination
                $big = 999999999; // need an unlikely integer
                $links = paginate_links( array(
                    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
                    'format' => '?paged=%#%',
                    'current' => max( 1, get_query_var('paged') ),
                    'total' => $wp_query->max_num_pages,
                    'prev_text'    => '<',
                    'next_text'    => '>',
                    'type' => 'array'
                ) );
                if(!empty($links)){ ?>
                <ul class="pagination">
                        <?php

                        foreach($links as $link){
                            ?>
                            <li><?php echo $link; ?></li>
                            <?php
                        }
                        wp_reset_query(); ?>
                    </ul>
                    <?php } ?>
相关问题