帖子页面上的Wordpress分页

时间:2014-11-26 15:56:13

标签: php wordpress

我真的很难找到将分页符引入我的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

我不确定在脚本中我需要添加哪些脚本,我已经尝试了解在codex中的哪些示例与我如何利用我的内容有关。

3 个答案:

答案 0 :(得分: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,

           ) );

      }

  }



}

您可以在类别blog.php中使用它,例如:

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

</div>

答案 1 :(得分:1)

您需要将分页参数添加到args数组中。从页面查询var中检索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的框中的分页链接。

答案 2 :(得分:0)

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

<?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 etting中我设置了&#39;阅读&#39; “博客”页面最多只显示&#39;并将其设置为4以反映上述内容,嘿它出现了!