【问题标题】:Wordpress Pagination on posts page帖子页面上的 Wordpress 分页
【发布时间】:2014-11-26 15:56:13
【问题描述】:

我真的很难找出在哪里或如何在我的 wordpess 帖子页面中引入分页...

<?php
    if (is_tag()) {
        $args = array('tag_id' => get_queried_object_id());
    } else {
        $args = array('cat' => get_queried_object_id());
    }

    $i=1;
    $latest_blog_posts = new WP_Query( $args  );

    if ( $latest_blog_posts->have_posts() ) : while ( $latest_blog_posts->have_posts() ) : $latest_blog_posts->the_post();

    foreach((get_the_category()) as $category) {
        $cat_name = $category->cat_name;
        $cat_link = get_category_link( $category->term_id );
    }

    $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 2600,1000 ), false, '' );
?> 

<div style="height: auto; overflow: auto;">
    <div style="float: left; margin-right: 15px;">   
        <div style="background: url(<?php echo $src[0]; ?> ); width: 330px; height: 260px; background-size: cover; background-repeat:no-repeat;">
        </div>
    </div>

    <div style="float: right; width: 600px;"> 
        <h2 style="margin:0; padding: 0;"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
        <p><span style="font-weight: 200; color: #aaa;">| <?php the_time('j F Y'); ?></span></p>
        <p style="line-height: 160%;"><?php echo substr(get_the_excerpt(), 0,500); ?> [...]</p>

        <?php echo get_the_tag_list('<p> | ','&nbsp;&nbsp; | ','</p>'); ?>
        <?php $archive_year = get_the_time('Y'); ?>
    </div>
</div>

<?php if ( has_post_thumbnail() ) : ?>
<?php else: ?>
<?php endif; ?>
<?php $i++; endwhile; endif;    ?> 

该文件具体命名为 category-blog.php

我不确定我需要在分页中添加的脚本中的哪个位置,我试图了解示例在法典中的哪个位置与我如何使用我的内容有关。

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    也许这对你有帮助,我使用此代码,将其添加到 functions.php

    if ( !function_exists( 'wpex_pagination' ) ) {
    
    
    
      function wpex_pagination() {
    
    
    
          $prev_arrow = is_rtl() ? '&rarr;' : '&larr;';
    
          $next_arrow = is_rtl() ? '&larr;' : '&rarr;';
    
    
    
          global $wp_query;
    
          $total = $wp_query->max_num_pages;
    
          $big = 999999999; // need an unlikely integer
    
          if( $total > 0 )  {
    
               if( !$current_page = get_query_var('paged') )
    
                   $current_page = 1;
    
               if( get_option('permalink_structure') ) {
    
                   $format = 'page/%#%/';
    
               } else {
    
                   $format = '&paged=%#%';
    
               }
    
              echo paginate_links(array(
    
                  'base'          => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    
                  'format'        => $format,
    
                  'current'       => max( 1, get_query_var('paged') ),
    
                  'total'         => $total,
    
                  'mid_size'      => 3,
    
                  'type'          => 'list',
    
                  'prev_text'     => $prev_arrow,
    
                  'next_text'     => $next_arrow,
    
               ) );
    
          }
    
      }
    
    
    
    }
    

    您可以在您的 category-blog.php 中使用它,例如:

    <div class="pagination">
        <?php wpex_pagination(); ?>
    
    </div>
    

    【讨论】:

    • 将该内容放入我的函数文件中时出现语法错误...
    • hm 代码中没有语法错误,请确保不要粘贴到任何 if else 或函数中...您可以将其粘贴到文件的最后 ?&gt;closure php 标签之前
    • 太奇怪了,我错过了一个结束标签。但是现在没有语法错误,但是当我在 div 中放置时什么都没有显示,我上面的代码中是否有特定的地方需要放这个?
    • 我添加了背景颜色,它显示 div 存在,但 div 中根本没有显示任何内容
    【解决方案2】:

    您需要将 paged 参数添加到您的 args 数组中。 paged 变量是从页面查询变量中检索到的,您可以这样获得:

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

    然后您可能希望使用posts_per_page 参数限制每页的帖子数。所以你的 args 数组会是这样的:

    $args = array(
        'paged' => $paged,
        'posts_per_page' => 5
    );
    

    Wordpress 有一个内置的函数来显示分页链接,它非常适合称为 paginate_links http://codex.wordpress.org/Function_Reference/paginate_links

    【讨论】:

    • 我真的很难找到在我上面的代码中的位置,因为我之前已经设法得到它,但是因为它没有真正正常工作而被删除,我的意思是它显示第 1 页和第 2 页,但如果您单击链接转到第 2 页,则无法显示任何帖子并吓坏了...
    • 我已经添加了 arg 并且它工作正常,所以它只显示我想要多少帖子。我现在无法引入分页。
    • 你在使用 paginate_links 函数吗?
    【解决方案3】:

    这就是我修复它的方法...

    <?php
    
    $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
    
    if (is_tag()) {
    $args = array(
    'tag_id' => get_queried_object_id(),
    'posts_per_page' => 4,
    'paged' => $paged
    );
    
    } else {
    $args = array(
    'cat' => get_queried_object_id(),
    'posts_per_page' => 4,
    'paged' => $paged
    );
    
    }
    
    $i=1;
    
    $latest_blog_posts = new WP_Query( $args  );
    
    if ( $latest_blog_posts->have_posts() ) : while ( $latest_blog_posts->have_posts() ) : $latest_blog_posts->the_post();
    
    
    
    foreach((get_the_category()) as $category) {
       $cat_name = $category->cat_name;
       $cat_link = get_category_link( $category->term_id );
    }
    
    $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 2600,1000 ), false, '' );
    
    ?>
    
    
     <?php endif; ?>
    
    
    <div style="height: auto; overflow: auto; margin-bottom: 20px;">
    
        <div style="float: left; margin-right: 15px;">   
    
            <div style="background: url(<?php echo $src[0]; ?> ); width: 330px; height: 260px; background-size: cover; background-repeat:no-repeat;"></div>
    
        </div>
    
            <div style="float: right; width: 600px;"> 
    
                <h2 style="margin:0; padding: 0;"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    
                    <p><span style="font-weight: 200; color: #aaa;">| <?php the_time('j F Y'); ?></span></p>
    
                        <p style="line-height: 160%;"><?php echo substr(get_the_excerpt(), 0,500); ?> [...]</p>
    
                        <?php echo get_the_tag_list('<p> | ','&nbsp;&nbsp; | ','</p>'); ?>
    
                        <?php $archive_year = get_the_time('Y'); ?>
    
            </div>
    
    </div>
    
    <?php if ( has_post_thumbnail() ) : ?>
    
    <?php else: ?>
    
    <?php endif; ?>
    
    <?php $i++; endwhile; endif;    ?> 
    
    <?php
    
    
    $big = 999999999; // need an unlikely integer
    
    echo paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $latest_blog_posts->max_num_pages
    ) );
    ?>
    

    同样在 wp-admins 设置中,我在“阅读”下设置了“博客页面最多显示”并将其设置为 4 以反映上述内容,嘿,它突然出现了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 2011-03-14
      • 1970-01-01
      • 2019-03-05
      相关资源
      最近更新 更多