Wordpress - 添加"下一步"和"以前"指向自定义博客页面

时间:2016-01-06 12:01:36

标签: php wordpress twitter-bootstrap

我希望添加一个"下一个"和"以前"链接到我在Wordpress中构建的自定义博客页面。我设置的是来自Bootstrap的自定义网格,但我想将此网格限制为每页12个帖子(出于明显的优化原因)。我是一名前端开发人员,所以对我来说制作PHP后端有点棘手。

如何在我的博客页面中添加查询以包含下一个和上一个链接?我尝试了很多不同的解决方案,但似乎无法使用我的循环。

谢谢!

以下代码:



<?php 
/*
	Template Name: Blog Page
*/
?>
<?php get_header(); ?>

<header class="entry-header">
<h1 class="entry-title"><?php echo get_the_title(); ?></h1>
</header>


<div class="container">
<?php
  $args=array(
     'post_type' => 'post',
     'post_status' => 'publish',
     'posts_per_page' => 12
    );


  $my_query = null;
  $my_query = new WP_Query($args);

  if( $my_query->have_posts() ) {

  $i = 0;
  while ($my_query->have_posts()) : $my_query->the_post();
  if($i % 3 == 0) { ?> 

  <div class="row">

  <?php
  }
  ?>

    <div class="col-md-4">
      	<h4><a href="<?php the_permalink(); ?>" title="Read more"><?php the_title(); ?></a></h4>
		<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( array(200,150) ); ?></a>
		<?php the_excerpt(); ?>
    </div>  

      <?php $i++; 
      if($i != 0 && $i % 3 == 0) { ?>
        </div><!--/.row-->
        <div class="clearfix"></div>

      <?php
       } ?>

      <?php  
        endwhile;
        }
        wp_reset_query();
       ?>


</div><!--/.container-->  

<?php get_footer(); ?>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

paginate_links挂钩与自定义WP_Query数组结合使用。确保为查询指定paged数组参数。这会设置查询以返回分页结果。

  <?php
    // 1- Setup paging parameter
    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;

    // 2- Setup WP_query variable to get last 12 posts
    $args = array(
      'posts_per_page' => 12,
      'orderby' => 'most_recent',
      'paged' => $paged,
    );
    $the_query = new WP_Query( $args );

    // 3- Setup new loop
    if($the_query->have_posts()) : 
      while($the_query->have_posts()) : 
        $the_query->the_post();

    // 4- Output parameters you want here
        echo '<div class="col-md-4">';
        echo '<h4><a href="' . the_permalink() . 'title="Read more">' . the_title() . '</a></h4>';
        echo '<a href="' . the_permalink() . '">' . the_post_thumbnail( array(200,150) ) . '</a>';
        echo the_excerpt();
        echo '</div>';

    // 5- close up loop
      endwhile;
    endif;

    // 6- Output paginate_links just below post loop
    echo paginate_links( array(
      'base' => str_replace( 999999, '%#%', esc_url( get_pagenum_link( 999999 ) ) ),
      'format' => '?paged=%#%',
      'current' => max( 1, get_query_var('paged') ),
      'total' => $the_query->max_num_pages
    ) );

    // 7- reset post data query
    wp_reset_postdata(); 
  ?>

答案 1 :(得分:0)

这是一个非常常见的问题 - 您可以在https://codex.wordpress.org/Function_Reference/paginate_links

找到所有信息

也有一些例子你可以正确使用

相关问题