【问题标题】:building an archive page for wordpress with pagination使用分页为 wordpress 建立存档页面
【发布时间】:2015-09-18 21:03:05
【问题描述】:

我需要为大约 30.000 个帖子建立一个存档页面。我尝试使用 wordpress 的标准存档方法,但它并没有完全涵盖我想要做的事情。

这是我目前所拥有的:

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

<div id="container">
<div id="content" role="main">

    <?php
        // Get total number of posts for pagination
        $count_posts = wp_count_posts();
        $published_posts = $count_posts->publish;

        $args = array(
            'post_type' => 'post',
            'posts_per_page' => 50
        );

        $post_query = new WP_Query($args);
        ?>
        <table style="width:100%">
        <tr>
            <th>Post</th>
            <th>Datum</th>
            <th>Categorie</th>
        </tr>
        <?php

        if($post_query->have_posts() ) {
            while($post_query->have_posts() ) {
                echo "<tr>";
                $post_query->the_post();
                ?>
                <td><a href="<?php the_permalink() ?>" rel="bookmark" title="Link naar <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                <?php
                echo "<td>" . get_the_date() . "</td>";
                $category_list = get_the_category_list( ', ' );
                if ( $category_list )
                    echo "<td>" . $category_list . "</td>";
                echo "</tr>";
            }
        }
        echo "</table>";
    ?>

</div><!-- #content -->

现在我将所有帖子都放在一个页面上,这并不容易使用。因此,我需要进行分页以提供更多概述。我知道这一定是可能的,但我真的不知道该怎么做。

真的希望将来有人可以帮助我和某些人。也许还有另一种我找不到的解决方案。

【问题讨论】:

    标签: php jquery wordpress pagination


    【解决方案1】:

    改变参数:

    $args = array(
        'post_type' => 'post',
        'posts_per_page' => 50,
        'paged' => 1
    );
    

    对于分页,这应该可以工作:

    while( $post_query->have_posts() ){
        // your code
    }
    next_posts_link( 'Older Entries', $post_query ->max_num_pages );
    previous_posts_link( 'Newer Entries' );
    

    WordPress 分页documentation.

    您可以阅读 documentation 以获取自定义 WordPress 查询。

    【讨论】:

      【解决方案2】:

      请使用此代码:

      <?php
          $args = array(
              'post_type' => 'post',
              'posts_per_page' => 50
          );
      
          $post_query = new WP_Query($args);
      
          if($post_query->have_posts() ) {
              while($post_query->have_posts() ) {
                  echo "<tr>";
                  $post_query->the_post();
                  ?>
                  <td><a href="<?php the_permalink() ?>" rel="bookmark" title="Link naar <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                  <?php
                  echo "<td>" . get_the_date() . "</td>";
                  $category_list = get_the_category_list( ', ' );
                  if ( $category_list )
                      echo "<td>" . $category_list . "</td>";
                  echo "</tr>";
              }
          }
          echo "</table>";
      
         pagination_nav();
      ?>
      

      现在在你主题的functions.php中定义这个分页函数

      function pagination_nav(){
      // Don't print empty markup if there's only one page.
      if ( $GLOBALS['wp_query']->max_num_pages < 2 ) {
          return;
      }
      
      $paged        = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1;
      $pagenum_link = html_entity_decode( get_pagenum_link() );
      $query_args   = array();
      $url_parts    = explode( '?', $pagenum_link );
      
      if ( isset( $url_parts[1] ) ) {
          wp_parse_str( $url_parts[1], $query_args );
      }
      
      $pagenum_link = remove_query_arg( array_keys( $query_args ), $pagenum_link );
      $pagenum_link = trailingslashit( $pagenum_link ) . '%_%';
      
      $format  = $GLOBALS['wp_rewrite']->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : '';
      $format .= $GLOBALS['wp_rewrite']->using_permalinks() ? user_trailingslashit( 'page/%#%', 'paged' ) : '?paged=%#%';
      
      // Set up paginated links.
      $links = paginate_links( array(
          'base'     => $pagenum_link,
          'format'   => $format,
          'total'    => $GLOBALS['wp_query']->max_num_pages,
          'current'  => $paged,
          'mid_size' => 3,
          'add_args' => array_map( 'urlencode', $query_args ),
          'prev_text' => __( '&larr; Previous', '' ),
          'next_text' => __( 'Next &rarr;', '' ),
          'type'      => 'list',
      ) );
      if ( $links ) :
          echo $links;
      endif;
          }
      

      【讨论】:

      • 这似乎不起作用。我的存档页面中没有显示任何内容。它也不会出错。
      • 你在使用 $args.刚刚编辑了帖子并添加了 $args
      猜你喜欢
      • 1970-01-01
      • 2011-08-01
      • 1970-01-01
      • 2012-08-27
      • 1970-01-01
      • 2018-10-04
      • 2013-03-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多