[英]Wordpress Pagination on posts page
我真的很難找出在哪里或如何在我的wordposts頁面上介紹分頁...
<?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> | ',' | ','</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
我不確定在分頁中需要在腳本中的哪個位置進行添加,我試圖從法典中的位置了解示例與我如何利用我的內容相關聯。
也許這對您有幫助,我使用此代碼,將其添加到functions.php
if ( !function_exists( 'wpex_pagination' ) ) {
function wpex_pagination() {
$prev_arrow = is_rtl() ? '→' : '←';
$next_arrow = is_rtl() ? '←' : '→';
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>
您需要將分頁的參數添加到args數組中。 paged
變量是從頁面查詢var中檢索的,您可以像這樣獲得:
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
然后,您可能想使用posts_per_page
參數限制每頁的posts_per_page
數。 因此,您的args數組將如下所示:
$args = array(
'paged' => $paged,
'posts_per_page' => 5
);
Wordpress具有一個內置的功能來顯示分頁鏈接,該鏈接非常好地顯示在名為paginate_links
http://codex.wordpress.org/Function_Reference/paginate_links的框中。
所以這是我的解決方法...
<?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> | ',' | ','</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以反映上述情況,嘿,它出現了!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.